mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-09 11:35:30 +00:00
HTTPCLIENT-1303: support for java.net.Authenticator backed crednetials provider
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1483846 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
87d5a1dce9
commit
45456cf1f0
@ -542,7 +542,8 @@ public CloseableHttpClient build() {
|
|||||||
if (userAgent == null) {
|
if (userAgent == null) {
|
||||||
if (systemProperties) {
|
if (systemProperties) {
|
||||||
userAgent = System.getProperty("http.agent");
|
userAgent = System.getProperty("http.agent");
|
||||||
} else {
|
}
|
||||||
|
if (userAgent == null) {
|
||||||
final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client",
|
final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client",
|
||||||
HttpClientBuilder.class.getClassLoader());
|
HttpClientBuilder.class.getClassLoader());
|
||||||
final String release = vi != null ? vi.getRelease() : VersionInfo.UNAVAILABLE;
|
final String release = vi != null ? vi.getRelease() : VersionInfo.UNAVAILABLE;
|
||||||
@ -669,7 +670,11 @@ public CloseableHttpClient build() {
|
|||||||
|
|
||||||
CredentialsProvider defaultCredentialsProvider = this.credentialsProvider;
|
CredentialsProvider defaultCredentialsProvider = this.credentialsProvider;
|
||||||
if (defaultCredentialsProvider == null) {
|
if (defaultCredentialsProvider == null) {
|
||||||
defaultCredentialsProvider = new BasicCredentialsProvider();
|
if (systemProperties) {
|
||||||
|
defaultCredentialsProvider = new SystemDefaultCredentialsProvider();
|
||||||
|
} else {
|
||||||
|
defaultCredentialsProvider = new BasicCredentialsProvider();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new InternalHttpClient(
|
return new InternalHttpClient(
|
||||||
|
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.apache.http.impl.client;
|
||||||
|
|
||||||
|
import java.net.Authenticator;
|
||||||
|
import java.net.PasswordAuthentication;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import org.apache.http.annotation.ThreadSafe;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.Credentials;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.CredentialsProvider;
|
||||||
|
import org.apache.http.client.config.AuthSchemes;
|
||||||
|
import org.apache.http.util.Args;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of {@link CredentialsProvider} backed by system {@link Authenticator}.
|
||||||
|
*
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
@ThreadSafe
|
||||||
|
public class SystemDefaultCredentialsProvider implements CredentialsProvider {
|
||||||
|
|
||||||
|
private static Map<String, String> SCHEME_MAP;
|
||||||
|
|
||||||
|
static {
|
||||||
|
SCHEME_MAP = new ConcurrentHashMap<String, String>();
|
||||||
|
SCHEME_MAP.put(AuthSchemes.BASIC.toUpperCase(Locale.ENGLISH), "Basic");
|
||||||
|
SCHEME_MAP.put(AuthSchemes.DIGEST.toUpperCase(Locale.ENGLISH), "Digest");
|
||||||
|
SCHEME_MAP.put(AuthSchemes.NTLM.toUpperCase(Locale.ENGLISH), "NTLM");
|
||||||
|
SCHEME_MAP.put(AuthSchemes.SPNEGO.toUpperCase(Locale.ENGLISH), "SPNEGO");
|
||||||
|
SCHEME_MAP.put(AuthSchemes.KERBEROS.toUpperCase(Locale.ENGLISH), "Kerberos");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String translateScheme(final String key) {
|
||||||
|
if (key == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final String s = SCHEME_MAP.get(key);
|
||||||
|
return s != null ? s : key;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final BasicCredentialsProvider internal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor.
|
||||||
|
*/
|
||||||
|
public SystemDefaultCredentialsProvider() {
|
||||||
|
super();
|
||||||
|
this.internal = new BasicCredentialsProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
|
||||||
|
internal.setCredentials(authscope, credentials);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
final PasswordAuthentication systemcreds = Authenticator.requestPasswordAuthentication(
|
||||||
|
authscope.getHost(),
|
||||||
|
null,
|
||||||
|
authscope.getPort(),
|
||||||
|
"http",
|
||||||
|
null,
|
||||||
|
translateScheme(authscope.getScheme()));
|
||||||
|
if (systemcreds != null) {
|
||||||
|
return new UsernamePasswordCredentials(
|
||||||
|
systemcreds.getUserName(), new String(systemcreds.getPassword()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
internal.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user