本文整理匯總了Java中org.apache.http.impl.client.BasicAuthCache類的典型用法代碼示例。如果您正苦於以下問題:Java BasicAuthCache類的具體用法?Java BasicAuthCache怎麽用?Java BasicAuthCache使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BasicAuthCache類屬於org.apache.http.impl.client包,在下文中一共展示了BasicAuthCache類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addPreemptiveAuthenticationProxy
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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);
}
}
示例2: addPreemptiveAuthenticationProxy
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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);
}
}
示例3: createHttpClientContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
/**
* 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;
}
示例4: BitcoindApiHandler
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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);
}
示例5: checkLocalContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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);
}
}
示例6: checkLocalContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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);
}
}
示例7: configureContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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;
}
示例8: buildHttpContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
/**
* 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;
}
示例9: createClient
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
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();
}
示例10: withAuthentication
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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;
}
示例11: getRequestContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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;
}
示例12: createContext
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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;
}
示例13: testPreemptiveTargetAndProxyAuth
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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());
}
示例14: testCredentialsProviderNotSet
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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());
}
示例15: testAuthCacheEmpty
import org.apache.http.impl.client.BasicAuthCache; //導入依賴的package包/類
@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());
}