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

Commit fd4f6ac

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 fd4f6ac

File tree

2 files changed

+20
-5
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

+20
-5
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ private String calculateAuthentication(HttpAuthenticationFilter.Credentials cred
9292
* Adds authentication information to the request.
9393
*
9494
* @param request Request context.
95-
* @throws RequestAuthenticationException in case that basic credentials missing or are in invalid format
9695
*/
97-
public void filterRequest(ClientRequestContext request) throws RequestAuthenticationException {
96+
public void filterRequest(ClientRequestContext request) {
9897
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter.getCredentials(request,
9998
defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
100-
if (credentials == null) {
101-
throw new RequestAuthenticationException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
99+
if (credentials != null) {
100+
request.getHeaders().add(HttpHeaders.AUTHORIZATION, calculateAuthentication(credentials));
102101
}
103-
request.getHeaders().add(HttpHeaders.AUTHORIZATION, calculateAuthentication(credentials));
104102
}
105103

106104
/**

0 commit comments

Comments
 (0)