Skip to content

Commit 6381b88

Browse files
committed
Create Jwk and Jwt token store beans conditionally
Closes gh-9777
1 parent 0f8a819 commit 6381b88

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,14 @@ public JwkTokenStoreConfiguration(ResourceServerProperties resource) {
223223

224224
@Bean
225225
@ConditionalOnMissingBean(ResourceServerTokenServices.class)
226-
public DefaultTokenServices jwkTokenServices() {
226+
public DefaultTokenServices jwkTokenServices(TokenStore jwkTokenStore) {
227227
DefaultTokenServices services = new DefaultTokenServices();
228-
services.setTokenStore(jwkTokenStore());
228+
services.setTokenStore(jwkTokenStore);
229229
return services;
230230
}
231231

232232
@Bean
233+
@ConditionalOnMissingBean(TokenStore.class)
233234
public TokenStore jwkTokenStore() {
234235
return new JwkTokenStore(this.resource.getJwk().getKeySetUri());
235236
}
@@ -255,13 +256,14 @@ public JwtTokenServicesConfiguration(ResourceServerProperties resource,
255256

256257
@Bean
257258
@ConditionalOnMissingBean(ResourceServerTokenServices.class)
258-
public DefaultTokenServices jwtTokenServices() {
259+
public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
259260
DefaultTokenServices services = new DefaultTokenServices();
260-
services.setTokenStore(jwtTokenStore());
261+
services.setTokenStore(jwtTokenStore);
261262
return services;
262263
}
263264

264265
@Bean
266+
@ConditionalOnMissingBean(TokenStore.class)
265267
public TokenStore jwtTokenStore() {
266268
return new JwtTokenStore(jwtTokenEnhancer());
267269
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfigurationTests.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
5858
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
5959
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
60+
import org.springframework.security.oauth2.provider.token.TokenStore;
6061
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
62+
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
63+
import org.springframework.security.oauth2.provider.token.store.jwk.JwkTokenStore;
6164
import org.springframework.social.connect.ConnectionFactoryLocator;
6265
import org.springframework.stereotype.Component;
6366
import org.springframework.web.client.RestTemplate;
@@ -256,6 +259,25 @@ public void jwtAccessTokenConverterIsConfiguredWhenKeyUriIsProvided() {
256259
assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
257260
}
258261

262+
@Test
263+
public void jwkTokenStoreShouldBeConditionalOnMissingBean() throws Exception {
264+
EnvironmentTestUtils.addEnvironment(this.environment,
265+
"security.oauth2.resource.jwk.key-set-uri=http://my-auth-server/token_keys");
266+
this.context = new SpringApplicationBuilder(JwkTokenStoreConfiguration.class,
267+
ResourceConfiguration.class)
268+
.environment(this.environment).web(false).run();
269+
assertThat(this.context.getBeansOfType(JwkTokenStore.class)).hasSize(1);
270+
}
271+
272+
@Test
273+
public void jwtTokenStoreShouldBeConditionalOnMissingBean() throws Exception {
274+
EnvironmentTestUtils.addEnvironment(this.environment,
275+
"security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY);
276+
this.context = new SpringApplicationBuilder(JwtTokenStoreConfiguration.class, ResourceConfiguration.class)
277+
.environment(this.environment).web(false).run();
278+
assertThat(this.context.getBeansOfType(JwtTokenStore.class)).hasSize(1);
279+
}
280+
259281
@Configuration
260282
@Import({ ResourceServerTokenServicesConfiguration.class,
261283
ResourceServerPropertiesConfiguration.class,
@@ -380,6 +402,26 @@ public JwtAccessTokenConverterRestTemplateCustomizer restTemplateCustomizer() {
380402

381403
}
382404

405+
@Configuration
406+
static class JwtTokenStoreConfiguration {
407+
408+
@Bean
409+
public TokenStore tokenStore(JwtAccessTokenConverter jwtTokenEnhancer) {
410+
return new JwtTokenStore(jwtTokenEnhancer);
411+
}
412+
413+
}
414+
415+
@Configuration
416+
static class JwkTokenStoreConfiguration {
417+
418+
@Bean
419+
public TokenStore tokenStore() {
420+
return new JwkTokenStore("http://my.key-set.uri");
421+
}
422+
423+
}
424+
383425
private static class MockRestCallCustomizer
384426
implements JwtAccessTokenConverterRestTemplateCustomizer {
385427

0 commit comments

Comments
 (0)