Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 5cd1b51

Browse files
tsachevjzheaux
authored andcommitted
Make JwtAccessTokenConverter conditional
With this change users can override the JwtAccessTokenConverter used by the auto configured JwtTokenStore and their own enhancer. Fixes #231
1 parent 9fc0aad commit 5cd1b51

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

spring-security-oauth2-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/authserver/AuthorizationServerTokenServicesConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
7979

8080
@Bean
8181
@ConditionalOnMissingBean(TokenStore.class)
82-
public TokenStore jwtTokenStore() {
83-
return new JwtTokenStore(jwtTokenEnhancer());
82+
public TokenStore jwtTokenStore(JwtAccessTokenConverter jwtTokenEnhancer) {
83+
return new JwtTokenStore(jwtTokenEnhancer);
8484
}
8585

8686
@Bean
87+
@ConditionalOnMissingBean(JwtAccessTokenConverter.class)
8788
public JwtAccessTokenConverter jwtTokenEnhancer() {
8889
String keyValue = this.authorization.getJwt().getKeyValue();
8990
Assert.notNull(this.authorization.getJwt().getKeyValue(), "keyValue cannot be null");
@@ -137,11 +138,12 @@ public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
137138

138139
@Bean
139140
@ConditionalOnMissingBean(TokenStore.class)
140-
public TokenStore tokenStore() {
141-
return new JwtTokenStore(accessTokenConverter());
141+
public TokenStore tokenStore(JwtAccessTokenConverter accessTokenConverter) {
142+
return new JwtTokenStore(accessTokenConverter);
142143
}
143144

144145
@Bean
146+
@ConditionalOnMissingBean(JwtAccessTokenConverter.class)
145147
public JwtAccessTokenConverter accessTokenConverter() {
146148
Assert.notNull(this.authorization.getJwt().getKeyStore(), "keyStore cannot be null");
147149
Assert.notNull(this.authorization.getJwt().getKeyStorePassword(), "keyStorePassword cannot be null");

spring-security-oauth2-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/authserver/AuthorizationServerTokenServicesConfigurationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import org.springframework.boot.autoconfigure.AutoConfigurations;
2929
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3030
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
31+
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Configuration;
3233
import org.springframework.context.annotation.Import;
3334
import org.springframework.core.io.ClassPathResource;
35+
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
3436
import org.springframework.security.oauth2.provider.token.TokenStore;
3537
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
3638
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
@@ -120,11 +122,54 @@ public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() {
120122
.run(context -> assertThat(context).getFailure().isInstanceOf(UnsatisfiedDependencyException.class));
121123
}
122124

125+
@Test
126+
public void configureWhenPrivateKeyIsProvidedWithCustomJwtAccessTokenConverterThenDefaultBackoff()
127+
throws Exception {
128+
Path privateKeyPath = new ClassPathResource("key.private", this.getClass()).getFile().toPath();
129+
String privateKey = Files.readAllLines(privateKeyPath).stream().collect(Collectors.joining("\n"));
130+
131+
this.contextRunner.withUserConfiguration(JwtAccessTokenConverterConfiguration.class)
132+
.withPropertyValues("security.oauth2.authorization.jwt.key-value=" + privateKey).run(context -> {
133+
JwtAccessTokenConverter converter = context.getBean(JwtAccessTokenConverter.class);
134+
assertThat(converter.getAccessTokenConverter()).isInstanceOf(CustomAccessTokenConverter.class);
135+
});
136+
}
137+
138+
@Test
139+
public void configureWhenKeyStoreIsProvidedWithKeyPasswordAndCustomJwtAccessTokenConverterThenDefaultBackoff() {
140+
this.contextRunner.withUserConfiguration(JwtAccessTokenConverterConfiguration.class)
141+
.withPropertyValues("security.oauth2.authorization.jwt.key-store=classpath:"
142+
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keyhaspassword.jks",
143+
"security.oauth2.authorization.jwt.key-store-password=changeme",
144+
"security.oauth2.authorization.jwt.key-alias=jwt",
145+
"security.oauth2.authorization.jwt.key-password=password")
146+
.run(context -> {
147+
JwtAccessTokenConverter converter = context.getBean(JwtAccessTokenConverter.class);
148+
assertThat(converter.getAccessTokenConverter()).isInstanceOf(CustomAccessTokenConverter.class);
149+
});
150+
}
151+
123152
@Configuration
124153
@Import({ AuthorizationServerTokenServicesConfiguration.class })
125154
@EnableConfigurationProperties(AuthorizationServerProperties.class)
126155
protected static class AuthorizationServerConfiguration {
127156

128157
}
129158

159+
@Configuration
160+
protected static class JwtAccessTokenConverterConfiguration {
161+
162+
@Bean
163+
JwtAccessTokenConverter accessTokenConverter() {
164+
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
165+
converter.setAccessTokenConverter(new CustomAccessTokenConverter());
166+
return converter;
167+
}
168+
169+
}
170+
171+
protected static class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
172+
173+
}
174+
130175
}

0 commit comments

Comments
 (0)