@Test public void testConnectionCloseAfterAuthenticationSuccess() throws Exception { this.localServer.register("*", new ClosingAuthHandler()); this.localServer.start(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test", "test")); TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy(); this.httpclient.setCredentialsProvider(credsProvider); this.httpclient.setTargetAuthenticationStrategy(authStrategy); HttpContext context = new BasicHttpContext(); HttpHost targethost = getServerHttp(); for (int i = 0; i < 2; i++) { HttpGet httpget = new HttpGet("/"); HttpResponse response = this.httpclient.execute(targethost, httpget, context); EntityUtils.consume(response.getEntity()); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); } }
@Test public void testPreemptiveAuthenticationFailure() throws Exception { CountingAuthHandler requestHandler = new CountingAuthHandler(); this.localServer.register("*", requestHandler); this.localServer.start(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test", "stuff")); this.httpclient.setCredentialsProvider(credsProvider); HttpHost targethost = getServerHttp(); HttpContext context = new BasicHttpContext(); AuthCache authCache = new BasicAuthCache(); authCache.put(targethost, new BasicScheme()); context.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); HttpResponse response1 = this.httpclient.execute(targethost, httpget, context); HttpEntity entity1 = response1.getEntity(); Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getStatusLine().getStatusCode()); Assert.assertNotNull(entity1); EntityUtils.consume(entity1); Assert.assertEquals(1, requestHandler.getCount()); }
@Test public void testBasicAuthenticationCredentialsCaching() throws Exception { this.localServer.register("*", new AuthHandler()); this.localServer.start(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test", "test")); TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy(); this.httpclient.setCredentialsProvider(credsProvider); this.httpclient.setTargetAuthenticationStrategy(authStrategy); HttpContext context = new BasicHttpContext(); HttpHost targethost = getServerHttp(); HttpGet httpget = new HttpGet("/"); HttpResponse response1 = this.httpclient.execute(targethost, httpget, context); HttpEntity entity1 = response1.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode()); Assert.assertNotNull(entity1); EntityUtils.consume(entity1); HttpResponse response2 = this.httpclient.execute(targethost, httpget, context); HttpEntity entity2 = response1.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode()); Assert.assertNotNull(entity2); EntityUtils.consume(entity2); Assert.assertEquals(1, authStrategy.getCount()); }
public Credentials getCredentials(final AuthScope authscope) { Args.notNull(authscope, "Auth scope"); final Credentials localcreds = internal.getCredentials(authscope); if (localcreds != null) { return localcreds; } if (authscope.getHost() != null) { PasswordAuthentication systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.SERVER); if (systemcreds == null) { systemcreds = getSystemCreds(authscope, Authenticator.RequestorType.PROXY); } if (systemcreds != null) { final String domain = System.getProperty("http.auth.ntlm.domain"); if (domain != null) { return new NTCredentials( systemcreds.getUserName(), new String(systemcreds.getPassword()), null, domain); } else { if (AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())) { // Domian may be specified in a fully qualified user name return new NTCredentials( systemcreds.getUserName(), new String(systemcreds.getPassword()), null, null); } else { return new UsernamePasswordCredentials( systemcreds.getUserName(), new String(systemcreds.getPassword())); } } } } return null; }
public void setCredentials(final AuthScope authscope, final Credentials credentials) { internal.setCredentials(authscope, credentials); }
public void clear() { internal.clear(); }