Skip to content

Commit 460fdaf

Browse files
ayudovinsnicoll
authored andcommitted
Add configurable property for JWK encryption algorithm
See gh-15145
1 parent 5674a53 commit 460fdaf

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public static class Jwt {
4040
*/
4141
private String jwkSetUri;
4242

43+
/**
44+
* JSON Web Algorithm used for verifying the digital signatures.
45+
*/
46+
private String jwsAlgorithm = "RS256";
47+
4348
/**
4449
* URI that an OpenID Connect Provider asserts as its Issuer Identifier.
4550
*/
@@ -53,6 +58,14 @@ public void setJwkSetUri(String jwkSetUri) {
5358
this.jwkSetUri = jwkSetUri;
5459
}
5560

61+
public String getJwsAlgorithm() {
62+
return this.jwsAlgorithm;
63+
}
64+
65+
public void setJwsAlgorithm(String jwsAlgorithm) {
66+
this.jwsAlgorithm = jwsAlgorithm;
67+
}
68+
5669
public String getIssuerUri() {
5770
return this.issuerUri;
5871
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerJwkConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class OAuth2ResourceServerJwkConfiguration {
4646
@ConditionalOnProperty(name = "spring.security.oauth2.resourceserver.jwt.jwk-set-uri")
4747
@ConditionalOnMissingBean
4848
public JwtDecoder jwtDecoderByJwkKeySetUri() {
49-
return new NimbusJwtDecoderJwkSupport(this.properties.getJwt().getJwkSetUri());
49+
return new NimbusJwtDecoderJwkSupport(this.properties.getJwt().getJwkSetUri(),
50+
this.properties.getJwt().getJwsAlgorithm());
5051
}
5152

5253
@Bean

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import javax.servlet.Filter;
2424

25+
import com.nimbusds.jose.JWSAlgorithm;
2526
import okhttp3.mockwebserver.MockResponse;
2627
import okhttp3.mockwebserver.MockWebServer;
2728
import org.junit.After;
@@ -78,8 +79,26 @@ public void autoConfigurationShouldConfigureResourceServer() {
7879
this.contextRunner.withPropertyValues(
7980
"spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://jwk-set-uri.com")
8081
.run((context) -> {
81-
assertThat(context.getBean(JwtDecoder.class))
82-
.isInstanceOf(NimbusJwtDecoderJwkSupport.class);
82+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
83+
assertThat(jwtDecoder).isInstanceOf(NimbusJwtDecoderJwkSupport.class);
84+
NimbusJwtDecoderJwkSupport decoder = (NimbusJwtDecoderJwkSupport) jwtDecoder;
85+
assertThat(decoder).hasFieldOrPropertyWithValue("jwsAlgorithm",
86+
JWSAlgorithm.RS256);
87+
assertThat(getBearerTokenFilter(context)).isNotNull();
88+
});
89+
}
90+
91+
@Test
92+
public void autoConfigurationShouldConfigureResourceServerWithJwsAlgotihms() {
93+
this.contextRunner.withPropertyValues(
94+
"spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://jwk-set-uri.com",
95+
"spring.security.oauth2.resourceserver.jwt.jws-algorithm=HS512")
96+
.run((context) -> {
97+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
98+
assertThat(jwtDecoder).isInstanceOf(NimbusJwtDecoderJwkSupport.class);
99+
NimbusJwtDecoderJwkSupport decoder = (NimbusJwtDecoderJwkSupport) jwtDecoder;
100+
assertThat(decoder).hasFieldOrPropertyWithValue("jwsAlgorithm",
101+
JWSAlgorithm.HS512);
83102
assertThat(getBearerTokenFilter(context)).isNotNull();
84103
});
85104
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ content into your application. Rather, pick only the properties that you need.
547547
548548
# SECURITY OAUTH2 RESOURCE SERVER ({sc-spring-boot-autoconfigure}/security/oauth2/resource/OAuth2ResourceServerProperties.{sc-ext}[OAuth2ResourceServerProperties])
549549
spring.security.oauth2.resourceserver.jwt.jwk-set-uri= # JSON Web Key URI to use to verify the JWT token.
550+
spring.security.oauth2.resourceserver.jwt.jws-algorithm= # JSON Web Algorithm used for verifying the digital signatures.
550551
spring.security.oauth2.resourceserver.jwt.issuer-uri= # URI that an OpenID Connect Provider asserts as its Issuer Identifier.
551552
552553
# ----------------------------------------

0 commit comments

Comments
 (0)