|
35 | 35 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
36 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
37 | 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
| 38 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
38 | 39 | import org.springframework.boot.security.autoconfigure.servlet.UserDetailsServiceAutoConfiguration;
|
39 | 40 | import org.springframework.context.annotation.Bean;
|
40 | 41 | import org.springframework.context.annotation.Configuration;
|
|
48 | 49 | * OAuth2 authorization server that require it (e.g. User Info, Client Registration).
|
49 | 50 | *
|
50 | 51 | * @author Steve Riesenberg
|
| 52 | + * @author Yanming Zhou |
51 | 53 | * @since 4.0.0
|
52 | 54 | */
|
53 | 55 | @AutoConfiguration(after = UserDetailsServiceAutoConfiguration.class)
|
54 | 56 | @ConditionalOnClass({ OAuth2Authorization.class, JWKSource.class })
|
55 | 57 | @ConditionalOnWebApplication(type = Type.SERVLET)
|
| 58 | +@EnableConfigurationProperties(OAuth2AuthorizationServerProperties.class) |
56 | 59 | public final class OAuth2AuthorizationServerJwtAutoConfiguration {
|
57 | 60 |
|
58 | 61 | @Bean
|
59 | 62 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
60 | 63 | @ConditionalOnMissingBean
|
61 |
| - JWKSource<SecurityContext> jwkSource() { |
62 |
| - RSAKey rsaKey = getRsaKey(); |
| 64 | + JWKSource<SecurityContext> jwkSource(OAuth2AuthorizationServerProperties properties) { |
| 65 | + RSAKey rsaKey = getRsaKey(properties.getRsa()); |
63 | 66 | JWKSet jwkSet = new JWKSet(rsaKey);
|
64 | 67 | return new ImmutableJWKSet<>(jwkSet);
|
65 | 68 | }
|
66 | 69 |
|
67 |
| - private static RSAKey getRsaKey() { |
68 |
| - KeyPair keyPair = generateRsaKey(); |
69 |
| - RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
70 |
| - RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
71 |
| - RSAKey rsaKey = new RSAKey.Builder(publicKey).privateKey(privateKey) |
72 |
| - .keyID(UUID.randomUUID().toString()) |
73 |
| - .build(); |
| 70 | + private static RSAKey getRsaKey(OAuth2AuthorizationServerProperties.Rsa rsa) { |
| 71 | + RSAKey rsaKey; |
| 72 | + if (rsa.getPublicKey() != null && rsa.getPrivateKey() != null) { |
| 73 | + rsaKey = new RSAKey.Builder(rsa.getPublicKey()).privateKey(rsa.getPrivateKey()) |
| 74 | + .keyID(rsa.getKeyId() != null ? rsa.getKeyId() : UUID.randomUUID().toString()) |
| 75 | + .build(); |
| 76 | + } |
| 77 | + else { |
| 78 | + KeyPair keyPair = generateRsaKey(); |
| 79 | + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
| 80 | + RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
| 81 | + rsaKey = new RSAKey.Builder(publicKey).privateKey(privateKey).keyID(UUID.randomUUID().toString()).build(); |
| 82 | + } |
74 | 83 | return rsaKey;
|
75 | 84 | }
|
76 | 85 |
|
|
0 commit comments