Skip to content

Commit 8e1a9ff

Browse files
committed
Introduce RSA keys configuration properties for OAuth2 Authorization Server
Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 0c572ed commit 8e1a9ff

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

module/spring-boot-security-oauth2-authorization-server/src/main/java/org/springframework/boot/security/oauth2/server/authorization/autoconfigure/servlet/OAuth2AuthorizationServerJwtAutoConfiguration.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3636
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3737
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
38+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3839
import org.springframework.boot.security.autoconfigure.servlet.UserDetailsServiceAutoConfiguration;
3940
import org.springframework.context.annotation.Bean;
4041
import org.springframework.context.annotation.Configuration;
@@ -48,29 +49,37 @@
4849
* OAuth2 authorization server that require it (e.g. User Info, Client Registration).
4950
*
5051
* @author Steve Riesenberg
52+
* @author Yanming Zhou
5153
* @since 4.0.0
5254
*/
5355
@AutoConfiguration(after = UserDetailsServiceAutoConfiguration.class)
5456
@ConditionalOnClass({ OAuth2Authorization.class, JWKSource.class })
5557
@ConditionalOnWebApplication(type = Type.SERVLET)
58+
@EnableConfigurationProperties(OAuth2AuthorizationServerProperties.class)
5659
public final class OAuth2AuthorizationServerJwtAutoConfiguration {
5760

5861
@Bean
5962
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
6063
@ConditionalOnMissingBean
61-
JWKSource<SecurityContext> jwkSource() {
62-
RSAKey rsaKey = getRsaKey();
64+
JWKSource<SecurityContext> jwkSource(OAuth2AuthorizationServerProperties properties) {
65+
RSAKey rsaKey = getRsaKey(properties.getRsa());
6366
JWKSet jwkSet = new JWKSet(rsaKey);
6467
return new ImmutableJWKSet<>(jwkSet);
6568
}
6669

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+
}
7483
return rsaKey;
7584
}
7685

module/spring-boot-security-oauth2-authorization-server/src/main/java/org/springframework/boot/security/oauth2/server/authorization/autoconfigure/servlet/OAuth2AuthorizationServerProperties.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.security.oauth2.server.authorization.autoconfigure.servlet;
1818

19+
import java.security.interfaces.RSAPrivateKey;
20+
import java.security.interfaces.RSAPublicKey;
1921
import java.time.Duration;
2022
import java.util.HashMap;
2123
import java.util.HashSet;
@@ -35,6 +37,7 @@
3537
*
3638
* @author Steve Riesenberg
3739
* @author Florian Lemaire
40+
* @author Yanming Zhou
3841
* @since 4.0.0
3942
*/
4043
@ConfigurationProperties("spring.security.oauth2.authorizationserver")
@@ -62,6 +65,11 @@ public class OAuth2AuthorizationServerProperties implements InitializingBean {
6265
*/
6366
private final Endpoint endpoint = new Endpoint();
6467

68+
/**
69+
* Authorization Server endpoints.
70+
*/
71+
private final Rsa rsa = new Rsa();
72+
6573
public boolean isMultipleIssuersAllowed() {
6674
return this.multipleIssuersAllowed;
6775
}
@@ -86,6 +94,10 @@ public Endpoint getEndpoint() {
8694
return this.endpoint;
8795
}
8896

97+
public Rsa getRsa() {
98+
return this.rsa;
99+
}
100+
89101
@Override
90102
public void afterPropertiesSet() {
91103
validate();
@@ -567,4 +579,50 @@ public void setIdTokenSignatureAlgorithm(String idTokenSignatureAlgorithm) {
567579

568580
}
569581

582+
/**
583+
* RSA keys for JWK.
584+
*/
585+
public static class Rsa {
586+
587+
/**
588+
* RSA key ID.
589+
*/
590+
private @Nullable String keyId;
591+
592+
/**
593+
* RSA public key.
594+
*/
595+
private @Nullable RSAPublicKey publicKey;
596+
597+
/**
598+
* RSA private key.
599+
*/
600+
private @Nullable RSAPrivateKey privateKey;
601+
602+
public @Nullable String getKeyId() {
603+
return this.keyId;
604+
}
605+
606+
public void setKeyId(String keyId) {
607+
this.keyId = keyId;
608+
}
609+
610+
public @Nullable RSAPublicKey getPublicKey() {
611+
return this.publicKey;
612+
}
613+
614+
public void setPublicKey(RSAPublicKey publicKey) {
615+
this.publicKey = publicKey;
616+
}
617+
618+
public @Nullable RSAPrivateKey getPrivateKey() {
619+
return this.privateKey;
620+
}
621+
622+
public void setPrivateKey(RSAPrivateKey privateKey) {
623+
this.privateKey = privateKey;
624+
}
625+
626+
}
627+
570628
}

0 commit comments

Comments
 (0)