Skip to content

Commit cc69b08

Browse files
committed
Merge pull request #15145 from ayudovin
* pr/15145: Polish "Add configurable property for JWK encryption algorithm" Add configurable property for JWK encryption algorithm
2 parents 5674a53 + 0df13ba commit cc69b08

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
@Configuration
3737
class OAuth2ResourceServerJwkConfiguration {
3838

39-
private final OAuth2ResourceServerProperties properties;
39+
private final OAuth2ResourceServerProperties.Jwt properties;
4040

4141
OAuth2ResourceServerJwkConfiguration(OAuth2ResourceServerProperties properties) {
42-
this.properties = properties;
42+
this.properties = properties.getJwt();
4343
}
4444

4545
@Bean
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.getJwkSetUri(),
50+
this.properties.getJwsAlgorithm());
5051
}
5152

5253
@Bean
5354
@Conditional(IssuerUriCondition.class)
5455
@ConditionalOnMissingBean
5556
public JwtDecoder jwtDecoderByIssuerUri() {
56-
return JwtDecoders
57-
.fromOidcIssuerLocation(this.properties.getJwt().getIssuerUri());
57+
return JwtDecoders.fromOidcIssuerLocation(this.properties.getIssuerUri());
5858
}
5959

6060
}

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

Lines changed: 25 additions & 0 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;
@@ -84,6 +85,30 @@ public void autoConfigurationShouldConfigureResourceServer() {
8485
});
8586
}
8687

88+
@Test
89+
public void autoConfigurationShouldMatchDefaultJwsAlgorithm() {
90+
this.contextRunner.withPropertyValues(
91+
"spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://jwk-set-uri.com")
92+
.run((context) -> {
93+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
94+
assertThat(jwtDecoder).hasFieldOrPropertyWithValue("jwsAlgorithm",
95+
JWSAlgorithm.RS256);
96+
});
97+
}
98+
99+
@Test
100+
public void autoConfigurationShouldConfigureResourceServerWithJwsAlgorithm() {
101+
this.contextRunner.withPropertyValues(
102+
"spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://jwk-set-uri.com",
103+
"spring.security.oauth2.resourceserver.jwt.jws-algorithm=HS512")
104+
.run((context) -> {
105+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
106+
assertThat(jwtDecoder).hasFieldOrPropertyWithValue("jwsAlgorithm",
107+
JWSAlgorithm.HS512);
108+
assertThat(getBearerTokenFilter(context)).isNotNull();
109+
});
110+
}
111+
87112
@Test
88113
public void autoConfigurationShouldConfigureResourceServerUsingOidcIssuerUri()
89114
throws Exception {

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=RS256 # 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)