Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit a268b0c

Browse files
author
alessandro.gherardi
committed
Allow the same Jersey client to be used for both authenticated and non-authenticated resources
1 parent 1f46147 commit a268b0c

File tree

2 files changed

+19
-3
lines changed
  • connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector
  • core-client/src/main/java/org/glassfish/jersey/client/authentication

2 files changed

+19
-3
lines changed

connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector/AuthTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ public String getFilter(@Context HttpHeaders h) {
168168
return "GET";
169169
}
170170

171+
@GET
172+
@Path("noauth")
173+
public String get() {
174+
return "GET";
175+
}
176+
171177
@POST
172178
public String post(@Context HttpHeaders h, String e) {
173179
requestCount++;
@@ -259,6 +265,17 @@ public void testAuthGetWithClientFilter() {
259265
assertEquals("GET", r.request().get(String.class));
260266
}
261267

268+
@Test
269+
public void testAuthGetBasicNoChallenge() {
270+
ClientConfig cc = new ClientConfig();
271+
cc.connectorProvider(new ApacheConnectorProvider());
272+
Client client = ClientBuilder.newClient(cc);
273+
client.register(HttpAuthenticationFeature.basicBuilder().build());
274+
WebTarget r = client.target(getBaseUri()).path("test/noauth");
275+
276+
assertEquals("GET", r.request().get(String.class));
277+
}
278+
262279
@Test
263280
@Ignore("JERSEY-1750: Cannot retry request with a non-repeatable request entity. How to buffer the entity?"
264281
+ " Allow repeatable write in jersey?")

core-client/src/main/java/org/glassfish/jersey/client/authentication/BasicAuthenticator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ private String calculateAuthentication(HttpAuthenticationFilter.Credentials cred
9797
public void filterRequest(ClientRequestContext request) throws RequestAuthenticationException {
9898
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter.getCredentials(request,
9999
defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
100-
if (credentials == null) {
101-
throw new RequestAuthenticationException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
100+
if (credentials != null) {
101+
request.getHeaders().add(HttpHeaders.AUTHORIZATION, calculateAuthentication(credentials));
102102
}
103-
request.getHeaders().add(HttpHeaders.AUTHORIZATION, calculateAuthentication(credentials));
104103
}
105104

106105
/**

0 commit comments

Comments
 (0)