Annotation for cacheable auth schemes
This commit is contained in:
parent
668e1d04ce
commit
d88e32f952
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* 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.hc.client5.http.auth;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuthStateCacheable {
|
||||
}
|
|
@ -43,6 +43,7 @@ import org.apache.commons.codec.binary.Base64;
|
|||
import org.apache.hc.client5.http.auth.AuthChallenge;
|
||||
import org.apache.hc.client5.http.auth.AuthScheme;
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.AuthStateCacheable;
|
||||
import org.apache.hc.client5.http.auth.AuthenticationException;
|
||||
import org.apache.hc.client5.http.auth.Credentials;
|
||||
import org.apache.hc.client5.http.auth.CredentialsProvider;
|
||||
|
@ -59,6 +60,7 @@ import org.apache.hc.core5.util.Args;
|
|||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
@AuthStateCacheable
|
||||
public class BasicScheme implements AuthScheme, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1931571557597830536L;
|
||||
|
|
|
@ -39,11 +39,11 @@ import org.apache.hc.client5.http.auth.AuthCache;
|
|||
import org.apache.hc.client5.http.auth.AuthChallenge;
|
||||
import org.apache.hc.client5.http.auth.AuthExchange;
|
||||
import org.apache.hc.client5.http.auth.AuthScheme;
|
||||
import org.apache.hc.client5.http.auth.AuthStateCacheable;
|
||||
import org.apache.hc.client5.http.auth.AuthenticationException;
|
||||
import org.apache.hc.client5.http.auth.ChallengeType;
|
||||
import org.apache.hc.client5.http.auth.CredentialsProvider;
|
||||
import org.apache.hc.client5.http.auth.MalformedChallengeException;
|
||||
import org.apache.hc.client5.http.config.AuthSchemes;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.core5.http.FormattedHeader;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
|
@ -314,13 +314,9 @@ public class HttpAuthenticator {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean isCachable(final AuthScheme authScheme) {
|
||||
final String schemeName = authScheme.getName();
|
||||
return schemeName.equalsIgnoreCase(AuthSchemes.BASIC);
|
||||
}
|
||||
|
||||
private void updateCache(final HttpHost host, final AuthScheme authScheme, final HttpClientContext clientContext) {
|
||||
if (isCachable(authScheme)) {
|
||||
final boolean cachable = authScheme.getClass().getAnnotation(AuthStateCacheable.class) != null;
|
||||
if (cachable) {
|
||||
AuthCache authCache = clientContext.getAuthCache();
|
||||
if (authCache == null) {
|
||||
authCache = new BasicAuthCache();
|
||||
|
|
|
@ -34,11 +34,13 @@ import org.apache.hc.client5.http.auth.AuthExchange;
|
|||
import org.apache.hc.client5.http.auth.AuthScheme;
|
||||
import org.apache.hc.client5.http.auth.AuthSchemeProvider;
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.AuthStateCacheable;
|
||||
import org.apache.hc.client5.http.auth.AuthenticationException;
|
||||
import org.apache.hc.client5.http.auth.ChallengeType;
|
||||
import org.apache.hc.client5.http.auth.Credentials;
|
||||
import org.apache.hc.client5.http.auth.CredentialsProvider;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.config.AuthSchemes;
|
||||
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.core5.http.HttpHeaders;
|
||||
|
@ -56,13 +58,24 @@ import org.apache.hc.core5.http.protocol.HttpContext;
|
|||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
@SuppressWarnings({"boxing","static-access"})
|
||||
public class TestHttpAuthenticator {
|
||||
|
||||
@AuthStateCacheable
|
||||
abstract class CacheableAuthState implements AuthScheme {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return AuthSchemes.BASIC;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private AuthExchange authExchange;
|
||||
private AuthScheme authScheme;
|
||||
private CacheableAuthState authScheme;
|
||||
private HttpContext context;
|
||||
private HttpHost defaultHost;
|
||||
private CredentialsProvider credentialsProvider;
|
||||
|
@ -73,8 +86,8 @@ public class TestHttpAuthenticator {
|
|||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.authExchange = new AuthExchange();
|
||||
this.authScheme = Mockito.mock(AuthScheme.class);
|
||||
Mockito.when(this.authScheme.getName()).thenReturn("Basic");
|
||||
this.authScheme = Mockito.mock(CacheableAuthState.class, Mockito.withSettings()
|
||||
.defaultAnswer(Answers.CALLS_REAL_METHODS));
|
||||
Mockito.when(this.authScheme.isChallengeComplete()).thenReturn(Boolean.TRUE);
|
||||
this.context = new BasicHttpContext();
|
||||
this.defaultHost = new HttpHost("localhost", 80);
|
||||
|
|
Loading…
Reference in New Issue