use of org.apache.http.auth.Credentials in project platform_external_apache-http by android.
the class BasicCredentialsProvider method matchCredentials.
/**
* Find matching {@link Credentials credentials} for the given authentication scope.
*
* @param map the credentials hash map
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*
*/
private static Credentials matchCredentials(final HashMap<AuthScope, Credentials> map, final AuthScope authscope) {
// see if we get a direct hit
Credentials creds = map.get(authscope);
if (creds == null) {
// Nope.
// Do a full scan
int bestMatchFactor = -1;
AuthScope bestMatch = null;
for (AuthScope current : map.keySet()) {
int factor = authscope.match(current);
if (factor > bestMatchFactor) {
bestMatchFactor = factor;
bestMatch = current;
}
}
if (bestMatch != null) {
creds = map.get(bestMatch);
}
}
return creds;
}
use of org.apache.http.auth.Credentials in project maven-plugins by apache.
the class JavadocUtil method createHttpClient.
/**
* Creates a new {@code HttpClient} instance.
*
* @param settings The settings to use for setting up the client or {@code null}.
* @param url The {@code URL} to use for setting up the client or {@code null}.
*
* @return A new {@code HttpClient} instance.
*
* @see #DEFAULT_TIMEOUT
* @since 2.8
*/
private static HttpClient createHttpClient(Settings settings, URL url) {
DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT);
httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
// Some web servers don't allow the default user-agent sent by httpClient
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
if (settings != null && settings.getActiveProxy() != null) {
Proxy activeProxy = settings.getActiveProxy();
ProxyInfo proxyInfo = new ProxyInfo();
proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
if (StringUtils.isNotEmpty(activeProxy.getHost()) && (url == null || !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost()))) {
HttpHost proxy = new HttpHost(activeProxy.getHost(), activeProxy.getPort());
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
}
}
}
return httpClient;
}
use of org.apache.http.auth.Credentials in project jena by apache.
the class TestFusekiTestAuth method testServer_auth.
@Test
public void testServer_auth() {
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
Credentials credentials = new UsernamePasswordCredentials(USER, PASSWORD);
credsProvider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestAuth.urlDataset(), "*/*", client, null)) {
}
}
use of org.apache.http.auth.Credentials in project jmeter by apache.
the class HTTPHC4Impl method setConnectionAuthorization.
/**
* Setup credentials for url AuthScope but keeps Proxy AuthScope credentials
* @param client HttpClient
* @param url URL
* @param authManager {@link AuthManager}
* @param key key
*/
private void setConnectionAuthorization(CloseableHttpClient client, URL url, AuthManager authManager, HttpClientKey key) {
CredentialsProvider credentialsProvider = ((AbstractHttpClient) client).getCredentialsProvider();
if (authManager != null) {
if (authManager.hasAuthForURL(url)) {
authManager.setupCredentials(client, url, credentialsProvider, LOCALHOST);
} else {
credentialsProvider.clear();
}
} else {
Credentials credentials = null;
AuthScope authScope = null;
if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) {
authScope = new AuthScope(key.proxyHost, key.proxyPort);
credentials = credentialsProvider.getCredentials(authScope);
}
credentialsProvider.clear();
if (credentials != null) {
credentialsProvider.setCredentials(authScope, credentials);
}
}
}
use of org.apache.http.auth.Credentials in project lucene-solr by apache.
the class Krb5HttpClientBuilder method getBuilder.
public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
String configValue = System.getProperty(LOGIN_CONFIG_PROP);
if (configValue != null) {
logger.info("Setting up SPNego auth with config: " + configValue);
final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);
// authentication mechanism can load the credentials from the JAAS configuration.
if (useSubjectCredsVal == null) {
System.setProperty(useSubjectCredsProp, "false");
} else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
// Don't overwrite the prop value if it's already been written to something else,
// but log because it is likely the Credentials won't be loaded correctly.
logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal + " not false. SPNego authentication may not be successful.");
}
javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
//Enable only SPNEGO authentication scheme.
builder.setAuthSchemeRegistryProvider(() -> {
Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)).build();
return authProviders;
});
// Get the credentials from the JAAS configuration rather than here
Credentials useJaasCreds = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME);
builder.setCookieSpecRegistryProvider(() -> {
SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
Lookup<CookieSpecProvider> cookieRegistry = RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build();
return cookieRegistry;
});
builder.setDefaultCredentialsProvider(() -> {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds);
return credentialsProvider;
});
HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
}
} else {
logger.warn("{} is configured without specifying system property '{}'", getClass().getName(), LOGIN_CONFIG_PROP);
}
return builder;
}
Aggregations