Redesign of CredentialsProvider interface

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1687908 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2015-06-27 10:50:38 +00:00
parent d5c520a8e1
commit fb56b49465
27 changed files with 114 additions and 108 deletions

View File

@ -43,7 +43,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
@ -110,7 +110,7 @@ public class Executor {
private final HttpClient httpclient; private final HttpClient httpclient;
private volatile AuthCache authCache; private volatile AuthCache authCache;
private volatile CredentialsProvider credentialsProvider; private volatile CredentialsStore credentialsStore;
private volatile CookieStore cookieStore; private volatile CookieStore cookieStore;
Executor(final HttpClient httpclient) { Executor(final HttpClient httpclient) {
@ -122,16 +122,16 @@ public class Executor {
/** /**
* @since 4.5 * @since 4.5
*/ */
public Executor use(final CredentialsProvider credentialsProvider) { public Executor use(final CredentialsStore credentialsStore) {
this.credentialsProvider = credentialsProvider; this.credentialsStore = credentialsStore;
return this; return this;
} }
public Executor auth(final AuthScope authScope, final Credentials creds) { public Executor auth(final AuthScope authScope, final Credentials creds) {
if (this.credentialsProvider == null) { if (this.credentialsStore == null) {
this.credentialsProvider = new BasicCredentialsProvider(); this.credentialsStore = new BasicCredentialsProvider();
} }
this.credentialsProvider.setCredentials(authScope, creds); this.credentialsStore.setCredentials(authScope, creds);
return this; return this;
} }
@ -207,8 +207,8 @@ public class Executor {
} }
public Executor clearAuth() { public Executor clearAuth() {
if (this.credentialsProvider != null) { if (this.credentialsStore != null) {
this.credentialsProvider.clear(); this.credentialsStore.clear();
} }
return this; return this;
} }
@ -248,8 +248,8 @@ public class Executor {
public Response execute( public Response execute(
final Request request) throws ClientProtocolException, IOException { final Request request) throws ClientProtocolException, IOException {
final HttpClientContext localContext = HttpClientContext.create(); final HttpClientContext localContext = HttpClientContext.create();
if (this.credentialsProvider != null) { if (this.credentialsStore != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsStore);
} }
if (this.authCache != null) { if (this.authCache != null) {
localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache); localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);

View File

@ -31,7 +31,7 @@ import java.util.Map;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.osgi.services.ProxyConfiguration; import org.apache.http.osgi.services.ProxyConfiguration;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration; import org.osgi.framework.ServiceRegistration;
@ -39,7 +39,7 @@ import org.osgi.framework.ServiceRegistration;
/** /**
* @since 4.3 * @since 4.3
*/ */
final class OSGiCredentialsProvider implements CredentialsProvider { final class OSGiCredentialsProvider implements CredentialsStore {
private final BundleContext bundleContext; private final BundleContext bundleContext;

View File

@ -29,12 +29,12 @@ package org.apache.http.impl.auth.win;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.util.Args; import org.apache.http.util.Args;
/** /**
* {@link org.apache.http.client.CredentialsProvider} implementation that always returns * {@link CredentialsStore} implementation that always returns
* {@link org.apache.http.impl.auth.win.CurrentWindowsCredentials} instance to NTLM * {@link org.apache.http.impl.auth.win.CurrentWindowsCredentials} instance to NTLM
* and SPNego authentication challenges. * and SPNego authentication challenges.
* <p> * <p>
@ -44,11 +44,11 @@ import org.apache.http.util.Args;
* @since 4.4 * @since 4.4
*/ */
@ThreadSafe @ThreadSafe
public class WindowsCredentialsProvider implements CredentialsProvider { public class WindowsCredentialsProvider implements CredentialsStore {
private final CredentialsProvider provider; private final CredentialsStore provider;
public WindowsCredentialsProvider(final CredentialsProvider provider) { public WindowsCredentialsProvider(final CredentialsStore provider) {
this.provider = Args.notNull(provider, "Credentials provider"); this.provider = Args.notNull(provider, "Credentials provider");
} }

View File

@ -29,7 +29,7 @@ package org.apache.http.impl.client;
import java.util.Locale; import java.util.Locale;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider; import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder; import org.apache.http.config.RegistryBuilder;

View File

@ -28,10 +28,6 @@ package org.apache.http.impl.auth.win;
import java.io.IOException; import java.io.IOException;
import com.sun.jna.platform.win32.Sspi.CtxtHandle;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinError;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
@ -40,7 +36,7 @@ import org.apache.http.HttpStatus;
import org.apache.http.auth.AUTH; import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthScheme; import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider; import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -59,6 +55,11 @@ import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.sun.jna.platform.win32.Sspi.CtxtHandle;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinError;
/** /**
* Unit tests for Windows negotiate authentication. * Unit tests for Windows negotiate authentication.
*/ */

View File

@ -28,7 +28,6 @@ package org.apache.http.examples.client;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider;
@ -43,7 +42,7 @@ import org.apache.http.util.EntityUtils;
public class ClientAuthentication { public class ClientAuthentication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( credsProvider.setCredentials(
new AuthScope("localhost", 443), new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password")); new UsernamePasswordCredentials("username", "password"));

View File

@ -41,8 +41,8 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.ParseException; import org.apache.http.ParseException;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;

View File

@ -30,7 +30,6 @@ import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
@ -53,7 +52,7 @@ public class ClientPreemptiveBasicAuthentication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http"); HttpHost target = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()), new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password")); new UsernamePasswordCredentials("username", "password"));

View File

@ -30,7 +30,6 @@ import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
@ -54,7 +53,7 @@ public class ClientPreemptiveDigestAuthentication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http"); HttpHost target = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()), new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password")); new UsernamePasswordCredentials("username", "password"));

View File

@ -29,7 +29,6 @@ package org.apache.http.examples.client;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -45,7 +44,7 @@ import org.apache.http.util.EntityUtils;
public class ClientProxyAuthentication { public class ClientProxyAuthentication {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( credsProvider.setCredentials(
new AuthScope("localhost", 8080), new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("username", "password")); new UsernamePasswordCredentials("username", "password"));

View File

@ -0,0 +1,49 @@
/*
* ====================================================================
* 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.auth;
/**
* Provider of authentication credentials.
* <p>
* Implementations of this interface must be thread-safe. Access to shared
* data must be synchronized as methods of this interface may be executed
* from multiple threads.
*
* @since 4.0
*/
public interface CredentialsProvider {
/**
* Returns {@link Credentials credentials} for the given authentication scope,
* if available.
*
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*/
Credentials getCredentials(AuthScope authscope);
}

View File

@ -28,10 +28,10 @@ package org.apache.http.client;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
/** /**
* Abstract credentials provider that maintains a collection of user * Abstract store of authentication credentials.
* credentials.
* <p> * <p>
* Implementations of this interface must be thread-safe. Access to shared * Implementations of this interface must be thread-safe. Access to shared
* data must be synchronized as methods of this interface may be executed * data must be synchronized as methods of this interface may be executed
@ -39,7 +39,7 @@ import org.apache.http.auth.Credentials;
* *
* @since 4.0 * @since 4.0
*/ */
public interface CredentialsProvider { public interface CredentialsStore extends CredentialsProvider {
/** /**
* Sets the {@link Credentials credentials} for the given authentication * Sets the {@link Credentials credentials} for the given authentication
@ -53,16 +53,6 @@ public interface CredentialsProvider {
*/ */
void setCredentials(AuthScope authscope, Credentials credentials); void setCredentials(AuthScope authscope, Credentials credentials);
/**
* Get the {@link Credentials credentials} for the given authentication scope.
*
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*
* @see #setCredentials(AuthScope, Credentials)
*/
Credentials getCredentials(AuthScope authscope);
/** /**
* Clears all credentials. * Clears all credentials.
*/ */

View File

@ -33,9 +33,9 @@ import java.util.List;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthState; import org.apache.http.auth.AuthState;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup; import org.apache.http.config.Lookup;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
@ -94,7 +94,7 @@ public class HttpClientContext extends HttpCoreContext {
public static final String COOKIE_STORE = "http.cookie-store"; public static final String COOKIE_STORE = "http.cookie-store";
/** /**
* Attribute name of a {@link org.apache.http.client.CredentialsProvider} * Attribute name of a {@link CredentialsProvider}
* object that represents the actual credentials provider. * object that represents the actual credentials provider.
*/ */
public static final String CREDS_PROVIDER = "http.auth.credentials-provider"; public static final String CREDS_PROVIDER = "http.auth.credentials-provider";

View File

@ -41,8 +41,8 @@ import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState; import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.routing.RouteInfo; import org.apache.http.conn.routing.RouteInfo;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args; import org.apache.http.util.Args;

View File

@ -49,10 +49,10 @@ import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.MalformedChallengeException; import org.apache.http.auth.MalformedChallengeException;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.AuthenticationStrategy; import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;

View File

@ -32,16 +32,16 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.util.Args; import org.apache.http.util.Args;
/** /**
* Default implementation of {@link CredentialsProvider}. * Default implementation of {@link CredentialsStore}.
* *
* @since 4.0 * @since 4.0
*/ */
@ThreadSafe @ThreadSafe
public class BasicCredentialsProvider implements CredentialsProvider { public class BasicCredentialsProvider implements CredentialsStore {
private final ConcurrentHashMap<AuthScope, Credentials> credMap; private final ConcurrentHashMap<AuthScope, Credentials> credMap;

View File

@ -49,11 +49,11 @@ import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor; import org.apache.http.HttpResponseInterceptor;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthenticationStrategy; import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.BackoffManager; import org.apache.http.client.BackoffManager;
import org.apache.http.client.ConnectionBackoffStrategy; import org.apache.http.client.ConnectionBackoffStrategy;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.RedirectStrategy; import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.ServiceUnavailableRetryStrategy; import org.apache.http.client.ServiceUnavailableRetryStrategy;

View File

@ -39,9 +39,9 @@ import org.apache.http.HttpRequest;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthState; import org.apache.http.auth.AuthState;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.Configurable; import org.apache.http.client.methods.Configurable;

View File

@ -38,18 +38,18 @@ import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials; import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.util.Args; import org.apache.http.util.Args;
/** /**
* Implementation of {@link CredentialsProvider} backed by standard * Implementation of {@link CredentialsStore} backed by standard
* JRE {@link Authenticator}. * JRE {@link Authenticator}.
* *
* @since 4.3 * @since 4.3
*/ */
@ThreadSafe @ThreadSafe
public class SystemDefaultCredentialsProvider implements CredentialsProvider { public class SystemDefaultCredentialsProvider implements CredentialsStore {
private static final Map<String, String> SCHEME_MAP; private static final Map<String, String> SCHEME_MAP;

View File

@ -39,8 +39,9 @@ import org.apache.http.HttpRequest;
import org.apache.http.ProtocolException; import org.apache.http.ProtocolException;
import org.apache.http.annotation.Immutable; import org.apache.http.annotation.Immutable;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsStore;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpExecutionAware; import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;
@ -48,7 +49,6 @@ import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URIUtils;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.protocol.HttpCoreContext; import org.apache.http.protocol.HttpCoreContext;
import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessor;
import org.apache.http.util.Args; import org.apache.http.util.Args;
@ -142,14 +142,12 @@ public class ProtocolExec implements ClientExecChain {
if (uri != null) { if (uri != null) {
final String userinfo = uri.getUserInfo(); final String userinfo = uri.getUserInfo();
if (userinfo != null) { if (userinfo != null) {
CredentialsProvider credsProvider = context.getCredentialsProvider(); final CredentialsProvider credsProvider = context.getCredentialsProvider();
if (credsProvider == null) { if (credsProvider instanceof CredentialsStore) {
credsProvider = new BasicCredentialsProvider(); ((CredentialsStore) credsProvider).setCredentials(
context.setCredentialsProvider(credsProvider); new AuthScope(target),
new UsernamePasswordCredentials(userinfo));
} }
credsProvider.setCredentials(
new AuthScope(target),
new UsernamePasswordCredentials(userinfo));
} }
} }

View File

@ -41,9 +41,9 @@ import org.apache.http.auth.AuthOption;
import org.apache.http.auth.AuthScheme; import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
@ -227,7 +227,7 @@ public class TestAuthenticationStrategy {
.register("digest", new DigestSchemeFactory()).build(); .register("digest", new DigestSchemeFactory()).build();
context.setAuthSchemeRegistry(authSchemeRegistry); context.setAuthSchemeRegistry(authSchemeRegistry);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("somehost", 80, "realm2"), credentialsProvider.setCredentials(new AuthScope("somehost", 80, "realm2"),
new UsernamePasswordCredentials("user", "pwd")); new UsernamePasswordCredentials("user", "pwd"));
context.setCredentialsProvider(credentialsProvider); context.setCredentialsProvider(credentialsProvider);
@ -256,7 +256,7 @@ public class TestAuthenticationStrategy {
.register("digest", new DigestSchemeFactory()).build(); .register("digest", new DigestSchemeFactory()).build();
context.setAuthSchemeRegistry(authSchemeRegistry); context.setAuthSchemeRegistry(authSchemeRegistry);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("somehost", 80), credentialsProvider.setCredentials(new AuthScope("somehost", 80),
new UsernamePasswordCredentials("user", "pwd")); new UsernamePasswordCredentials("user", "pwd"));
context.setCredentialsProvider(credentialsProvider); context.setCredentialsProvider(credentialsProvider);
@ -291,7 +291,7 @@ public class TestAuthenticationStrategy {
context.setAuthSchemeRegistry(authSchemeRegistry); context.setAuthSchemeRegistry(authSchemeRegistry);
context.setRequestConfig(config); context.setRequestConfig(config);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("somehost", 80), credentialsProvider.setCredentials(new AuthScope("somehost", 80),
new UsernamePasswordCredentials("user", "pwd")); new UsernamePasswordCredentials("user", "pwd"));
context.setCredentialsProvider(credentialsProvider); context.setCredentialsProvider(credentialsProvider);

View File

@ -33,9 +33,9 @@ import java.util.Arrays;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;

View File

@ -41,10 +41,10 @@ import org.apache.http.HttpStatus;
import org.apache.http.auth.AUTH; import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache; import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.NonRepeatableRequestException; import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -149,20 +149,12 @@ public class TestClientAuthentication extends LocalServerTestBase {
this.creds = creds; this.creds = creds;
} }
@Override
public void clear() {
}
@Override @Override
public Credentials getCredentials(final AuthScope authscope) { public Credentials getCredentials(final AuthScope authscope) {
this.authscope = authscope; this.authscope = authscope;
return this.creds; return this.creds;
} }
@Override
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
}
public AuthScope getAuthScope() { public AuthScope getAuthScope() {
return this.authscope; return this.authscope;
} }

View File

@ -39,8 +39,8 @@ import org.apache.http.HttpStatus;
import org.apache.http.auth.AUTH; import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
@ -117,20 +117,12 @@ public class TestClientAuthenticationFallBack extends LocalServerTestBase {
this.creds = creds; this.creds = creds;
} }
@Override
public void clear() {
}
@Override @Override
public Credentials getCredentials(final AuthScope authscope) { public Credentials getCredentials(final AuthScope authscope) {
this.authscope = authscope; this.authscope = authscope;
return this.creds; return this.creds;
} }
@Override
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
}
public AuthScope getAuthScope() { public AuthScope getAuthScope() {
return this.authscope; return this.authscope;
} }

View File

@ -43,8 +43,8 @@ import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
@ -133,24 +133,12 @@ public class TestClientReauthentication extends LocalServerTestBase {
this.creds = creds; this.creds = creds;
} }
@Override
public void clear() {
}
@Override @Override
public Credentials getCredentials(final AuthScope authscope) { public Credentials getCredentials(final AuthScope authscope) {
this.authscope = authscope; this.authscope = authscope;
return this.creds; return this.creds;
} }
@Override
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
}
public AuthScope getAuthScope() {
return this.authscope;
}
} }
@Test @Test

View File

@ -38,7 +38,6 @@ import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
@ -155,7 +154,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final HttpHost target = start(); final HttpHost target = start();
final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager(); final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final Credentials use_jaas_creds = new UseJaasCredentials(); final Credentials use_jaas_creds = new UseJaasCredentials();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds); credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds);
@ -186,7 +185,7 @@ public class TestSPNegoScheme extends LocalServerTestBase {
final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager(); final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final Credentials use_jaas_creds = new UseJaasCredentials(); final Credentials use_jaas_creds = new UseJaasCredentials();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds); credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds);

View File

@ -33,13 +33,14 @@ import org.apache.http.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpExecutionAware; import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessor;
@ -185,11 +186,11 @@ public class TestProtocolExec {
final HttpRequestWrapper request = HttpRequestWrapper.wrap( final HttpRequestWrapper request = HttpRequestWrapper.wrap(
new HttpGet("http://somefella:secret@bar/test")); new HttpGet("http://somefella:secret@bar/test"));
final HttpClientContext context = HttpClientContext.create(); final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(new BasicCredentialsProvider());
protocolExec.execute(route, request, context, execAware); protocolExec.execute(route, request, context, execAware);
Assert.assertEquals(new URI("/test"), request.getURI()); Assert.assertEquals(new URI("/test"), request.getURI());
Assert.assertEquals(new HttpHost("bar", -1), context.getTargetHost()); Assert.assertEquals(new HttpHost("bar", -1), context.getTargetHost());
final CredentialsProvider credentialsProvider = context.getCredentialsProvider(); final CredentialsProvider credentialsProvider = context.getCredentialsProvider();
Assert.assertNotNull(credentialsProvider);
final Credentials creds = credentialsProvider.getCredentials(new AuthScope("bar", -1, null)); final Credentials creds = credentialsProvider.getCredentials(new AuthScope("bar", -1, null));
Assert.assertNotNull(creds); Assert.assertNotNull(creds);
Assert.assertEquals("somefella", creds.getUserPrincipal().getName()); Assert.assertEquals("somefella", creds.getUserPrincipal().getName());