make HttpClient configurable, closes #1071

pull/1079/merge
Julian Schlichtholz 2016-09-16 16:10:11 +02:00 committed by Justin Richer
parent f45a6ef56a
commit c3d0c18af5
9 changed files with 105 additions and 52 deletions

View File

@ -73,10 +73,15 @@ public class IntrospectingTokenService implements ResourceServerTokenServices {
private boolean cacheNonExpiringTokens = false; private boolean cacheNonExpiringTokens = false;
private boolean cacheTokens = true; private boolean cacheTokens = true;
private HttpClient httpClient = HttpClientBuilder.create() private HttpComponentsClientHttpRequestFactory factory;
.useSystemProperties()
.build(); public IntrospectingTokenService() {
private HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); this(HttpClientBuilder.create().useSystemProperties().build());
}
public IntrospectingTokenService(HttpClient httpClient) {
this.factory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
// Inner class to store in the hash map // Inner class to store in the hash map
private class TokenCacheObject { private class TokenCacheObject {

View File

@ -119,6 +119,8 @@ public class OIDCAuthenticationFilter extends AbstractAuthenticationProcessingFi
@Autowired(required=false) @Autowired(required=false)
private JWTSigningAndValidationService authenticationSignerService; private JWTSigningAndValidationService authenticationSignerService;
@Autowired(required=false)
private HttpClient httpClient;
/* /*
* Modular services to build out client filter. * Modular services to build out client filter.
@ -341,14 +343,14 @@ public class OIDCAuthenticationFilter extends AbstractAuthenticationProcessingFi
// Handle Token Endpoint interaction // Handle Token Endpoint interaction
HttpClient httpClient = HttpClientBuilder.create() if(httpClient == null) {
.useSystemProperties() httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig( .useSystemProperties()
RequestConfig.custom() .setDefaultRequestConfig(RequestConfig.custom()
.setSocketTimeout(httpSocketTimeout) .setSocketTimeout(httpSocketTimeout)
.build() .build())
) .build();
.build(); }
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

View File

@ -61,10 +61,14 @@ public class UserInfoFetcher {
private LoadingCache<PendingOIDCAuthenticationToken, UserInfo> cache; private LoadingCache<PendingOIDCAuthenticationToken, UserInfo> cache;
public UserInfoFetcher() { public UserInfoFetcher() {
this(HttpClientBuilder.create().useSystemProperties().build());
}
public UserInfoFetcher(HttpClient httpClient) {
cache = CacheBuilder.newBuilder() cache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch .expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch
.maximumSize(100) .maximumSize(100)
.build(new UserInfoLoader()); .build(new UserInfoLoader(httpClient));
} }
public UserInfo loadUserInfo(final PendingOIDCAuthenticationToken token) { public UserInfo loadUserInfo(final PendingOIDCAuthenticationToken token) {
@ -79,11 +83,12 @@ public class UserInfoFetcher {
private class UserInfoLoader extends CacheLoader<PendingOIDCAuthenticationToken, UserInfo> { private class UserInfoLoader extends CacheLoader<PendingOIDCAuthenticationToken, UserInfo> {
private HttpClient httpClient = HttpClientBuilder.create() private HttpComponentsClientHttpRequestFactory factory;
.useSystemProperties()
.build(); UserInfoLoader(HttpClient httpClient) {
private HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); this.factory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
public UserInfo load(final PendingOIDCAuthenticationToken token) { public UserInfo load(final PendingOIDCAuthenticationToken token) {
ServerConfiguration serverConfiguration = token.getServerConfiguration(); ServerConfiguration serverConfiguration = token.getServerConfiguration();

View File

@ -72,7 +72,11 @@ public class DynamicRegistrationClientConfigurationService implements ClientConf
private Set<String> blacklist = new HashSet<>(); private Set<String> blacklist = new HashSet<>();
public DynamicRegistrationClientConfigurationService() { public DynamicRegistrationClientConfigurationService() {
clients = CacheBuilder.newBuilder().build(new DynamicClientRegistrationLoader()); this(HttpClientBuilder.create().useSystemProperties().build());
}
public DynamicRegistrationClientConfigurationService(HttpClient httpClient) {
clients = CacheBuilder.newBuilder().build(new DynamicClientRegistrationLoader(httpClient));
} }
@Override @Override
@ -168,13 +172,17 @@ public class DynamicRegistrationClientConfigurationService implements ClientConf
* *
*/ */
public class DynamicClientRegistrationLoader extends CacheLoader<ServerConfiguration, RegisteredClient> { public class DynamicClientRegistrationLoader extends CacheLoader<ServerConfiguration, RegisteredClient> {
private HttpClient httpClient = HttpClientBuilder.create() private HttpComponentsClientHttpRequestFactory httpFactory;
.useSystemProperties()
.build();
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
private Gson gson = new Gson(); // note that this doesn't serialize nulls by default private Gson gson = new Gson(); // note that this doesn't serialize nulls by default
public DynamicClientRegistrationLoader() {
this(HttpClientBuilder.create().useSystemProperties().build());
}
public DynamicClientRegistrationLoader(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Override @Override
public RegisteredClient load(ServerConfiguration serverConfig) throws Exception { public RegisteredClient load(ServerConfiguration serverConfig) throws Exception {
RestTemplate restTemplate = new RestTemplate(httpFactory); RestTemplate restTemplate = new RestTemplate(httpFactory);

View File

@ -69,8 +69,12 @@ public class DynamicServerConfigurationService implements ServerConfigurationSer
private Set<String> blacklist = new HashSet<>(); private Set<String> blacklist = new HashSet<>();
public DynamicServerConfigurationService() { public DynamicServerConfigurationService() {
this(HttpClientBuilder.create().useSystemProperties().build());
}
public DynamicServerConfigurationService(HttpClient httpClient) {
// initialize the cache // initialize the cache
servers = CacheBuilder.newBuilder().build(new OpenIDConnectServiceConfigurationFetcher()); servers = CacheBuilder.newBuilder().build(new OpenIDConnectServiceConfigurationFetcher(httpClient));
} }
/** /**
@ -126,12 +130,13 @@ public class DynamicServerConfigurationService implements ServerConfigurationSer
* *
*/ */
private class OpenIDConnectServiceConfigurationFetcher extends CacheLoader<String, ServerConfiguration> { private class OpenIDConnectServiceConfigurationFetcher extends CacheLoader<String, ServerConfiguration> {
private HttpClient httpClient = HttpClientBuilder.create() private HttpComponentsClientHttpRequestFactory httpFactory;
.useSystemProperties()
.build();
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
private JsonParser parser = new JsonParser(); private JsonParser parser = new JsonParser();
OpenIDConnectServiceConfigurationFetcher(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Override @Override
public ServerConfiguration load(String issuer) throws Exception { public ServerConfiguration load(String issuer) throws Exception {
RestTemplate restTemplate = new RestTemplate(httpFactory); RestTemplate restTemplate = new RestTemplate(httpFactory);

View File

@ -94,7 +94,11 @@ public class WebfingerIssuerService implements IssuerService {
private boolean forceHttps = true; private boolean forceHttps = true;
public WebfingerIssuerService() { public WebfingerIssuerService() {
issuers = CacheBuilder.newBuilder().build(new WebfingerIssuerFetcher()); this(HttpClientBuilder.create().useSystemProperties().build());
}
public WebfingerIssuerService(HttpClient httpClient) {
issuers = CacheBuilder.newBuilder().build(new WebfingerIssuerFetcher(httpClient));
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -203,12 +207,13 @@ public class WebfingerIssuerService implements IssuerService {
* *
*/ */
private class WebfingerIssuerFetcher extends CacheLoader<String, LoadingResult> { private class WebfingerIssuerFetcher extends CacheLoader<String, LoadingResult> {
private HttpClient httpClient = HttpClientBuilder.create() private HttpComponentsClientHttpRequestFactory httpFactory;
.useSystemProperties()
.build();
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
private JsonParser parser = new JsonParser(); private JsonParser parser = new JsonParser();
WebfingerIssuerFetcher(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Override @Override
public LoadingResult load(String identifier) throws Exception { public LoadingResult load(String identifier) throws Exception {

View File

@ -68,11 +68,11 @@ public class JWKSetCacheService {
this.validators = CacheBuilder.newBuilder() this.validators = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch .expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch
.maximumSize(100) .maximumSize(100)
.build(new JWKSetVerifierFetcher()); .build(new JWKSetVerifierFetcher(HttpClientBuilder.create().useSystemProperties().build()));
this.encrypters = CacheBuilder.newBuilder() this.encrypters = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch .expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch
.maximumSize(100) .maximumSize(100)
.build(new JWKSetEncryptorFetcher()); .build(new JWKSetEncryptorFetcher(HttpClientBuilder.create().useSystemProperties().build()));
} }
/** /**
@ -104,9 +104,13 @@ public class JWKSetCacheService {
* *
*/ */
private class JWKSetVerifierFetcher extends CacheLoader<String, JWTSigningAndValidationService> { private class JWKSetVerifierFetcher extends CacheLoader<String, JWTSigningAndValidationService> {
private HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build(); private HttpComponentsClientHttpRequestFactory httpFactory;
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient); private RestTemplate restTemplate;
private RestTemplate restTemplate = new RestTemplate(httpFactory);
JWKSetVerifierFetcher(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
this.restTemplate = new RestTemplate(httpFactory);
}
/** /**
* Load the JWK Set and build the appropriate signing service. * Load the JWK Set and build the appropriate signing service.
@ -130,9 +134,14 @@ public class JWKSetCacheService {
* *
*/ */
private class JWKSetEncryptorFetcher extends CacheLoader<String, JWTEncryptionAndDecryptionService> { private class JWKSetEncryptorFetcher extends CacheLoader<String, JWTEncryptionAndDecryptionService> {
private HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build(); private HttpComponentsClientHttpRequestFactory httpFactory;
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient); private RestTemplate restTemplate;
private RestTemplate restTemplate = new RestTemplate(httpFactory);
public JWKSetEncryptorFetcher(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
this.restTemplate = new RestTemplate(httpFactory);
}
/* (non-Javadoc) /* (non-Javadoc)
* @see com.google.common.cache.CacheLoader#load(java.lang.Object) * @see com.google.common.cache.CacheLoader#load(java.lang.Object)
*/ */

View File

@ -103,7 +103,7 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
private LoadingCache<String, List<String>> sectorRedirects = CacheBuilder.newBuilder() private LoadingCache<String, List<String>> sectorRedirects = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS) .expireAfterAccess(1, TimeUnit.HOURS)
.maximumSize(100) .maximumSize(100)
.build(new SectorIdentifierLoader()); .build(new SectorIdentifierLoader(HttpClientBuilder.create().useSystemProperties().build()));
@Override @Override
public ClientDetailsEntity saveNewClient(ClientDetailsEntity client) { public ClientDetailsEntity saveNewClient(ClientDetailsEntity client) {
@ -465,11 +465,15 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
* *
*/ */
private class SectorIdentifierLoader extends CacheLoader<String, List<String>> { private class SectorIdentifierLoader extends CacheLoader<String, List<String>> {
private HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build(); private HttpComponentsClientHttpRequestFactory httpFactory;
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient); private RestTemplate restTemplate;
private RestTemplate restTemplate = new RestTemplate(httpFactory);
private JsonParser parser = new JsonParser(); private JsonParser parser = new JsonParser();
SectorIdentifierLoader(HttpClient httpClient) {
this.httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
this.restTemplate = new RestTemplate(httpFactory);
}
@Override @Override
public List<String> load(String key) throws Exception { public List<String> load(String key) throws Exception {

View File

@ -48,17 +48,20 @@ import com.google.common.util.concurrent.UncheckedExecutionException;
public class InMemoryClientLogoLoadingService implements ClientLogoLoadingService { public class InMemoryClientLogoLoadingService implements ClientLogoLoadingService {
private LoadingCache<ClientDetailsEntity, CachedImage> cache; private LoadingCache<ClientDetailsEntity, CachedImage> cache;
public InMemoryClientLogoLoadingService() {
this(HttpClientBuilder.create().useSystemProperties().build());
}
/** /**
* *
*/ */
public InMemoryClientLogoLoadingService() { public InMemoryClientLogoLoadingService(HttpClient httpClient) {
cache = CacheBuilder.newBuilder() cache = CacheBuilder.newBuilder()
.maximumSize(100) .maximumSize(100)
.expireAfterAccess(14, TimeUnit.DAYS) .expireAfterAccess(14, TimeUnit.DAYS)
.build(new ClientLogoFetcher()); .build(new ClientLogoFetcher(httpClient));
} }
@ -84,8 +87,15 @@ public class InMemoryClientLogoLoadingService implements ClientLogoLoadingServic
* *
*/ */
public class ClientLogoFetcher extends CacheLoader<ClientDetailsEntity, CachedImage> { public class ClientLogoFetcher extends CacheLoader<ClientDetailsEntity, CachedImage> {
private HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build(); private HttpClient httpClient;
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
public ClientLogoFetcher() {
this(HttpClientBuilder.create().useSystemProperties().build());
}
public ClientLogoFetcher(HttpClient httpClient) {
this.httpClient = httpClient;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see com.google.common.cache.CacheLoader#load(java.lang.Object) * @see com.google.common.cache.CacheLoader#load(java.lang.Object)