Java 类org.apache.http.client.AuthCache 实例源码
项目:remote-files-sync
文件:AuthenticationStrategyImpl.java
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(authScheme, "Auth scheme");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
if (isCachable(authScheme)) {
AuthCache authCache = clientContext.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
clientContext.setAuthCache(authCache);
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
项目:purecloud-iot
文件:TestClientAuthentication.java
@Test
public void testPreemptiveAuthentication() throws Exception {
final CountingAuthHandler requestHandler = new CountingAuthHandler();
this.serverBootstrap.registerHandler("*", requestHandler);
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
context.setAuthCache(authCache);
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity entity1 = response1.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
Assert.assertNotNull(entity1);
EntityUtils.consume(entity1);
Assert.assertEquals(1, requestHandler.getCount());
}
项目:nextcloud-java-api
文件:ConnectorCommon.java
private HttpClientContext prepareContext()
{
HttpHost targetHost = new HttpHost(serverConfig.getServerName(), serverConfig.getPort(), serverConfig.isUseHTTPS() ? "https" : "http");
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials
= new UsernamePasswordCredentials(serverConfig.getUserName(), serverConfig.getPassword());
credsProvider.setCredentials(AuthScope.ANY, credentials);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
return context;
}
项目:lams
文件:AuthenticationStrategyImpl.java
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
if (authhost == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (authScheme == null) {
throw new IllegalArgumentException("Auth scheme may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (isCachable(authScheme)) {
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
项目:lams
文件:AuthenticationStrategyImpl.java
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
if (authhost == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache != null) {
if (this.log.isDebugEnabled()) {
this.log.debug("Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
项目:aws-sdk-java-v2
文件:ApacheUtils.java
private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
ProxyConfiguration proxyConfiguration) {
if (proxyConfiguration.preemptiveBasicAuthenticationEnabled()) {
HttpHost targetHost = new HttpHost(proxyConfiguration.endpoint().getHost(), proxyConfiguration.endpoint().getPort());
final CredentialsProvider credsProvider = newProxyCredentialsProvider(proxyConfiguration);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
clientContext.setCredentialsProvider(credsProvider);
clientContext.setAuthCache(authCache);
}
}
项目:ibm-cos-sdk-java
文件:ApacheUtils.java
private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
HttpClientSettings settings) {
if (settings.isPreemptiveBasicProxyAuth()) {
HttpHost targetHost = new HttpHost(settings.getProxyHost(), settings
.getProxyPort());
final CredentialsProvider credsProvider = newProxyCredentialsProvider(settings);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
clientContext.setCredentialsProvider(credsProvider);
clientContext.setAuthCache(authCache);
}
}
项目:geoportal-server-harvester
文件:HttpClientContextBuilder.java
/**
* Creates client context.
* @param url url
* @param cred credentials
* @return client context
*/
public static HttpClientContext createHttpClientContext(URL url, SimpleCredentials cred) {
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(cred.getUserName(),cred.getPassword()));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
return context;
}
项目:BitcoindConnector4J
文件:BitcoindApiHandler.java
public BitcoindApiHandler(String host, int port, String protocol, String uri, String username, String password) {
this.uri = uri;
httpClient = HttpClients.createDefault();
targetHost = new HttpHost(host, port, protocol);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(username, password));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
}
项目:eSDK_EC_SDK_Java
文件:RestfulAdapterImplHttpClient.java
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
项目:eSDK_EC_SDK_Java
文件:RestfulAdapterImplHttpClientHTLS.java
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
项目:infiniboard
文件:UrlSourceJob.java
private HttpClientContext configureContext(CredentialsProvider credentialsProvider, String url) {
if (credentialsProvider != null) {
try {
URL targetUrl = new URL(url);
HttpHost targetHost =
new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
return context;
} catch (MalformedURLException e) {
LOG.error("Cannot parse URL '{}'", url, e);
}
}
return null;
}
项目:eSDK_IVS_Java
文件:RestfulAdapterImplHttpClient.java
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
项目:eSDK_IVS_Java
文件:RestfulAdapterImplHttpClientHTLS.java
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
项目:remote-files-sync
文件:AuthenticationStrategyImpl.java
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final AuthCache authCache = clientContext.getAuthCache();
if (authCache != null) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
项目:java-triton
文件:CloudApiApacheHttpClientContext.java
/**
* Builds a configured HTTP context object that is pre-configured for
* using HTTP Signature authentication.
*
* @param configurator HTTP Signatures configuration helper to pull properties from
* @return configured HTTP context object
*/
protected HttpContext buildHttpContext(final HttpSignatureConfigurator configurator) {
final HttpClientContext context = HttpClientContext.create();
if (configurator != null) {
AuthCache authCache = new BasicAuthCache();
context.setAuthCache(authCache);
AuthState authState = new AuthState();
authState.update(configurator.getAuthScheme(), configurator.getCredentials());
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE,
authState);
context.getTargetAuthState().setState(AuthProtocolState.UNCHALLENGED);
}
return context;
}
项目:ibm-bpm-rest-client
文件:SimpleBpmClient.java
private CloseableHttpClient createClient(String user, String password) {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(TOTAL_CONN);
cm.setDefaultMaxPerRoute(ROUTE_CONN);
logger.info("Pooling connection manager created.");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
logger.info("Default credentials provider created.");
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost(rootUri.getHost(), rootUri.getPort(), rootUri.getScheme()), basicAuth);
logger.info("Auth cache created.");
httpContext = HttpClientContext.create();
httpContext.setCredentialsProvider(credentialsProvider);
httpContext.setAuthCache(authCache);
logger.info("HttpContext filled with Auth cache.");
return HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(cm)
.build();
}
项目:boteco
文件:DefaultRestConfiguration.java
@Override
public RestConfiguration withAuthentication(String user, String password) {
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
CredentialsProvider provider = new BasicCredentialsProvider();
URI uri = request.getURI();
authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), basicAuth);
provider.setCredentials(new AuthScope(uri.getHost(), AuthScope.ANY_PORT),
new UsernamePasswordCredentials(user, password));
this.context.setCredentialsProvider(provider);
this.context.setAuthCache(authCache);
return this;
}
项目:communote-server
文件:ConfluenceUserImageProvider.java
@Override
protected HttpContext getRequestContext(URI imageUrl, String imageIdentifier,
ConfluenceConfiguration config) {
HttpHost targetHost = new HttpHost(imageUrl.getHost(), imageUrl.getPort());
CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider();
credentialsProviderProvider.setCredentials(new AuthScope(targetHost.getHostName(),
targetHost.getPort()), new UsernamePasswordCredentials(config.getAdminLogin(),
config.getAdminPassword()));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
// preemptive authentication (send credentials with request) by adding host to auth cache
// TODO maybe use real basic auth challenge?
authCache.put(targetHost, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProviderProvider);
context.setAuthCache(authCache);
return context;
}
项目:playground
文件:BasicAuthConfigurator.java
@Override
public BasicHttpContext createContext(HttpHost targetHost) {
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
// OPTIMIZED
credentialsProvider
.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// NON-OPTIMIZED
// credentialsProvider.setCredentials(
// new AuthScope(targetHost.getHostName(), targetHost.getPort()),
// new UsernamePasswordCredentials(username, password));
final BasicHttpContext context = new BasicHttpContext();
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
context.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsProvider);
return context;
}
项目:purecloud-iot
文件:AuthenticationStrategyAdaptor.java
@Override
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (isCachable(authScheme)) {
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
项目:purecloud-iot
文件:AuthenticationStrategyImpl.java
@Override
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(authScheme, "Auth scheme");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
if (isCachable(authScheme)) {
AuthCache authCache = clientContext.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
clientContext.setAuthCache(authCache);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
项目:purecloud-iot
文件:AuthenticationStrategyImpl.java
@Override
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final AuthCache authCache = clientContext.getAuthCache();
if (authCache != null) {
if (this.log.isDebugEnabled()) {
this.log.debug("Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
项目:purecloud-iot
文件:TestRequestAuthCache.java
@Test
public void testPreemptiveTargetAndProxyAuth() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.authscheme1);
authCache.put(this.proxy, this.authscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNotNull(this.targetState.getAuthScheme());
Assert.assertSame(this.creds1, this.targetState.getCredentials());
Assert.assertNotNull(this.proxyState.getAuthScheme());
Assert.assertSame(this.creds2, this.proxyState.getCredentials());
}
项目:purecloud-iot
文件:TestRequestAuthCache.java
@Test
public void testCredentialsProviderNotSet() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, null);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.authscheme1);
authCache.put(this.proxy, this.authscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNull(this.targetState.getAuthScheme());
Assert.assertNull(this.targetState.getCredentials());
Assert.assertNull(this.proxyState.getAuthScheme());
Assert.assertNull(this.proxyState.getCredentials());
}
项目:purecloud-iot
文件:TestRequestAuthCache.java
@Test
public void testAuthCacheEmpty() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNull(this.targetState.getAuthScheme());
Assert.assertNull(this.targetState.getCredentials());
Assert.assertNull(this.proxyState.getAuthScheme());
Assert.assertNull(this.proxyState.getCredentials());
}
项目:purecloud-iot
文件:TestRequestAuthCache.java
@Test
public void testNoMatchingCredentials() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.credProvider.clear();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.authscheme1);
authCache.put(this.proxy, this.authscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNull(this.targetState.getAuthScheme());
Assert.assertNull(this.targetState.getCredentials());
Assert.assertNull(this.proxyState.getAuthScheme());
Assert.assertNull(this.proxyState.getCredentials());
}
项目:purecloud-iot
文件:TestHttpAuthenticator.java
@Before
public void setUp() throws Exception {
this.defltAuthStrategy = Mockito.mock(AuthenticationStrategy.class);
this.authState = new AuthState();
this.authScheme = Mockito.mock(ContextAwareAuthScheme.class);
Mockito.when(this.authScheme.getSchemeName()).thenReturn("Basic");
Mockito.when(this.authScheme.isComplete()).thenReturn(Boolean.TRUE);
this.context = new BasicHttpContext();
this.defaultHost = new HttpHost("localhost", 80);
this.context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.defaultHost);
this.credentials = Mockito.mock(Credentials.class);
this.credentialsProvider = new BasicCredentialsProvider();
this.credentialsProvider.setCredentials(AuthScope.ANY, this.credentials);
this.context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
this.authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register("basic", new BasicSchemeFactory())
.register("digest", new DigestSchemeFactory())
.register("ntlm", new NTLMSchemeFactory()).build();
this.context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
this.authCache = Mockito.mock(AuthCache.class);
this.context.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
this.httpAuthenticator = new HttpAuthenticator();
}
项目:purecloud-iot
文件:TestClientAuthentication.java
@Test
public void testPreemptiveAuthenticationFailure() throws Exception {
final CountingAuthHandler requestHandler = new CountingAuthHandler();
this.serverBootstrap.registerHandler("*", requestHandler);
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
context.setAuthCache(authCache);
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("test", "stuff"));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity entity1 = response1.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getStatusLine().getStatusCode());
Assert.assertNotNull(entity1);
EntityUtils.consume(entity1);
Assert.assertEquals(1, requestHandler.getCount());
}
项目:purecloud-iot
文件:TestAuthenticationStrategy.java
@Test
public void testAuthScemeNonCacheable() throws Exception {
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
final HttpHost authhost = new HttpHost("somehost", 80);
final AuthScheme authScheme = Mockito.mock(AuthScheme.class);
Mockito.when(authScheme.isComplete()).thenReturn(true);
Mockito.when(authScheme.getSchemeName()).thenReturn("whatever");
final AuthCache authCache = Mockito.mock(AuthCache.class);
final HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
authStrategy.authSucceeded(authhost, authScheme, context);
Mockito.verify(authCache, Mockito.never()).put(authhost, authScheme);
}
项目:Visit
文件:AuthenticationStrategyImpl.java
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(authScheme, "Auth scheme");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
if (isCachable(authScheme)) {
AuthCache authCache = clientContext.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
clientContext.setAuthCache(authCache);
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
项目:Cytomine-client-autobuilder
文件:HttpClient.java
public void connect(String url, String username, String password) throws Exception {
isAuthByPrivateKey = false;
log.info("Connection to " + url + " with login=" + username + " and pass=" + password);
URL = new URL(url);
targetHost = new HttpHost(URL.getHost(), URL.getPort());
client = new DefaultHttpClient();
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
// Set credentials
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
}
项目:zee-jenkins
文件:RestClient.java
private HttpClientContext createClientContext(
String hostAddressWithProtocol, String userName, String password) {
URL url;
try {
url = new URL(hostAddressWithProtocol);
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(),
url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return context;
}
项目:dss
文件:CommonsDataLoader.java
protected HttpResponse getHttpResponse(final CloseableHttpClient client, final HttpUriRequest httpRequest, final String url) throws DSSException {
final String host = httpRequest.getURI().getHost();
final int port = httpRequest.getURI().getPort();
final String scheme = httpRequest.getURI().getScheme();
final HttpHost targetHost = new HttpHost(host, port, scheme);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
try {
final HttpResponse response = client.execute(targetHost, httpRequest, localContext);
return response;
} catch (IOException e) {
throw new DSSException(e);
}
}
项目:zfj-jenkins
文件:RestClient.java
private HttpClientContext createClientContext(
String hostAddressWithProtocol, String userName, String password) {
URL url;
try {
url = new URL(hostAddressWithProtocol);
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(),
url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return context;
}
项目:Cytomine-java-client
文件:HttpClient.java
public void connect(String url, String username, String password) throws IOException {
isAuthByPrivateKey = false;
log.info("Connection to " + url + " with login=" + username + " and pass=" + password);
URL = new URL(url);
targetHost = new HttpHost(URL.getHost(), URL.getPort());
client = HttpClientBuilder.create().build();
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Set credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
credsProvider.setCredentials(AuthScope.ANY, creds);
// Add AuthCache to the execution context
localcontext = HttpClientContext.create();
localcontext.setCredentialsProvider(credsProvider);
localcontext.setAuthCache(authCache);
}
项目:slack4gerrit
文件:HttpHelper.java
public static String getFromHttp(URL url, String user, String password) throws IOException
{
HttpClientContext context = HttpClientContext.create();
if (user != null)
{
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
}
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url.toExternalForm());
HttpResponse response = client.execute(request, context);
InputStreamReader streamReader = new InputStreamReader(response.getEntity().getContent());
String data = CharStreams.toString(streamReader);
streamReader.close();
return data;
}
项目:wiremock-velocity-transformer
文件:WireMockTestClient.java
public WireMockResponse getWithPreemptiveCredentials(String url, int port, String username, String password) {
HttpHost target = new HttpHost("localhost", port);
HttpClient httpClient = httpClientWithPreemptiveAuth(target, username, password);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(target, httpget, localContext);
return new WireMockResponse(response);
} catch (IOException e) {
return throwUnchecked(e, WireMockResponse.class);
}
}
项目:Lucee4
文件:HTTPEngine4Impl.java
public static BasicHttpContext setCredentials(DefaultHttpClient client, HttpHost httpHost, String username,String password, boolean preAuth) {
// set Username and Password
if(!StringUtil.isEmpty(username,true)) {
if(password==null)password="";
CredentialsProvider cp = client.getCredentialsProvider();
cp.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(username,password));
BasicHttpContext httpContext = new BasicHttpContext();
if(preAuth) {
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
return httpContext;
}
return null;
}
项目:Lucee4
文件:HTTPEngineImpl.java
public static BasicHttpContext setCredentials(HttpClientBuilder builder, HttpHost httpHost, String username,String password, boolean preAuth) {
// set Username and Password
if(!StringUtil.isEmpty(username,true)) {
if(password==null)password="";
CredentialsProvider cp = new BasicCredentialsProvider();
builder.setDefaultCredentialsProvider(cp);
cp.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(username,password));
BasicHttpContext httpContext = new BasicHttpContext();
if(preAuth) {
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
return httpContext;
}
return null;
}