Fixes #409 (Http client authentication with proxy server)
Made BasicResult a public static class so that it can be used by applications via AuthenticationStore.addAuthenticationResult().
This commit is contained in:
parent
3c0b654141
commit
be7c50fa51
|
@ -63,25 +63,41 @@ public class BasicAuthentication extends AbstractAuthentication
|
|||
@Override
|
||||
public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context)
|
||||
{
|
||||
String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
|
||||
return new BasicResult(headerInfo.getHeader(), value);
|
||||
return new BasicResult(getURI(), headerInfo.getHeader(), user, password);
|
||||
}
|
||||
|
||||
private class BasicResult implements Result
|
||||
/**
|
||||
* Basic authentication result.
|
||||
* <p>
|
||||
* Application may utilize this class directly via
|
||||
* {@link AuthenticationStore#addAuthenticationResult(Result)}
|
||||
* to perform preemptive authentication, that is immediately
|
||||
* sending the authorization header based on the fact that the
|
||||
* URI is known to require authentication and that username
|
||||
* and password are known a priori.
|
||||
*/
|
||||
public static class BasicResult implements Result
|
||||
{
|
||||
private final URI uri;
|
||||
private final HttpHeader header;
|
||||
private final String value;
|
||||
|
||||
public BasicResult(HttpHeader header, String value)
|
||||
public BasicResult(URI uri, String user, String password)
|
||||
{
|
||||
this(uri, HttpHeader.AUTHORIZATION, user, password);
|
||||
}
|
||||
|
||||
public BasicResult(URI uri, HttpHeader header, String user, String password)
|
||||
{
|
||||
this.uri = uri;
|
||||
this.header = header;
|
||||
this.value = value;
|
||||
this.value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI()
|
||||
{
|
||||
return BasicAuthentication.this.getURI();
|
||||
return uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -366,4 +366,33 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest
|
|||
|
||||
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_PreemptedAuthentication() throws Exception
|
||||
{
|
||||
startBasic(new EmptyServerHandler());
|
||||
|
||||
AuthenticationStore authenticationStore = client.getAuthenticationStore();
|
||||
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
|
||||
authenticationStore.addAuthenticationResult(new BasicAuthentication.BasicResult(uri, "basic", "basic"));
|
||||
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
client.getRequestListeners().add(new Request.Listener.Adapter()
|
||||
{
|
||||
@Override
|
||||
public void onSuccess(Request request)
|
||||
{
|
||||
requests.incrementAndGet();
|
||||
}
|
||||
});
|
||||
|
||||
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
||||
.scheme(scheme)
|
||||
.path("/secure")
|
||||
.timeout(5, TimeUnit.SECONDS)
|
||||
.send();
|
||||
|
||||
Assert.assertEquals(200, response.getStatus());
|
||||
Assert.assertEquals(1, requests.get());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue