下面列出了怎么用org.apache.http.impl.auth.DigestScheme的API类实例代码及写法,或者点击链接到github查看源代码。
private AuthScheme getAuthScheme() {
final String scheme = getProtocolScheme();
if (Constants.BASIC.equals(scheme)) {
return new BasicScheme();
} else if (Constants.DIGEST.equals(scheme)) {
return new DigestScheme();
} else if (Constants.NTLM.equals(scheme)) {
final Properties props = new Properties();
getWebConfig().getConfigParameterMap(ConfigName.CONFIG).entrySet().stream()
.filter(e -> e.getKey().startsWith(Config.JCIFS_PREFIX)).forEach(e -> {
props.setProperty(e.getKey(), e.getValue());
});
return new NTLMScheme(new JcifsEngine(props));
} else if (Constants.FORM.equals(scheme)) {
final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
return new FormScheme(parameterMap);
}
return null;
}
private HttpContext createHttpContext() {
// Create AuthCache instance
final AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
final DigestScheme digestAuth = new DigestScheme();
// If we already know the realm name
digestAuth.overrideParamter("realm", "Custom Realm Name");
// digestAuth.overrideParamter("nonce", "MTM3NTU2OTU4MDAwNzoyYWI5YTQ5MTlhNzc5N2UxMGM5M2Y5M2ViOTc4ZmVhNg==");
authCache.put(host, digestAuth);
// Add AuthCache to the execution context
final BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
return localcontext;
}
private AuthCache getAuthCache(AuthenticationType authenticationType, HttpHost target) {
AuthCache authCache = new BasicAuthCache();
if (authenticationType == AuthenticationType.BASIC) {
authCache.put(target, new BasicScheme());
} else {
authCache.put(target, new DigestScheme());
}
return authCache;
}
public HttpClientContext preemptive() {
AuthCache authCache = new BasicAuthCache();
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("realm", "");
digestAuth.overrideParamter("nonce", "");
// TODO : Add target
// authCache.put(target, digestAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
return localContext;
}
@Test
public void testInitializationCustomNoSslDigestAuth() throws Exception {
Properties props = new Properties();
props.setProperty(WebServicesFetcher.URL_KEY, "http://foo");
props.setProperty(WebServicesFetcher.APP_ID_KEY, "appId");
props.setProperty(WebServicesFetcher.KEYSTORE_FILE_KEY, "");
props.setProperty(WebServicesFetcher.KEYSTORE_PASSWORD_KEY, "");
props.setProperty(WebServicesFetcher.KEY_PASSWORD_KEY, "");
props.setProperty(WebServicesFetcher.TRUSTSTORE_FILE_KEY, "");
props.setProperty(WebServicesFetcher.TRUSTSTORE_PASSWORD_KEY, "");
props.setProperty(WebServicesFetcher.SUPPORTED_PROTOCOLS_KEY, "");
props.setProperty(WebServicesFetcher.HOSTNAME_VERIFIER_SKIP_KEY, "");
props.setProperty(WebServicesFetcher.MAX_CONCURRENT_CONNECTIONS_KEY, "1");
props.setProperty(WebServicesFetcher.VALIDATE_AFTER_INACTIVITY_KEY, "2");
props.setProperty(WebServicesFetcher.CONNECTION_TIMEOUT_KEY, "5000");
props.setProperty(WebServicesFetcher.NAME_SEPARATOR_KEY, "+");
props.setProperty(WebServicesFetcher.HTTP_AUTH_TYPE_KEY, "digest");
props.setProperty(WebServicesFetcher.HTTP_AUTH_USER_KEY, "user");
props.setProperty(WebServicesFetcher.HTTP_AUTH_PASSWORD_KEY, "password");
Configuration conf = createConfig(props);
WebServicesFetcher fetcher = new WebServicesFetcher();
try {
fetcher.init(conf);
Assert.assertNotNull(fetcher.getConfig());
Assert.assertEquals("http://foo", fetcher.getUrl());
Assert.assertEquals("appId", fetcher.getAppId());
Assert.assertEquals(5000, fetcher.getConnectionTimeout());
Assert.assertEquals("+", fetcher.getSeparator());
Assert.assertEquals("digest", fetcher.getHttpAuth());
Assert.assertEquals("user", fetcher.getHttpAuthUser());
Assert.assertEquals("password", fetcher.getHttpAuthPassword());
Assert.assertNotNull(fetcher.getCredentialsProvider());
Assert.assertEquals("user",
fetcher.getCredentialsProvider().getCredentials(AuthScope.ANY).getUserPrincipal().getName()
);
Assert.assertEquals("password", fetcher.getCredentialsProvider().getCredentials(AuthScope.ANY).getPassword());
Assert.assertTrue(fetcher.getAuthCache().get(new HttpHost(fetcher.getUrl())) instanceof DigestScheme);
PoolingHttpClientConnectionManager connectionManager = fetcher.getConnectionManager();
Assert.assertEquals(1, connectionManager.getMaxTotal());
Assert.assertEquals(1, connectionManager.getDefaultMaxPerRoute());
Assert.assertEquals(2, connectionManager.getValidateAfterInactivity());
Assert.assertNull(fetcher.getSslConnectionSocketFactory());
} finally {
fetcher.destroy();
}
}