Skip to content

Commit 9478abe

Browse files
committed
Internalize Nimbus JwtDecoder Builder
Issue: gh-6010
1 parent b935281 commit 9478abe

File tree

10 files changed

+334
-430
lines changed

10 files changed

+334
-430
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.springframework.security.web.util.matcher.RequestMatcher;
4646
import org.springframework.util.Assert;
4747

48-
import static org.springframework.security.oauth2.jwt.JwtProcessors.withJwkSetUri;
48+
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withJwkSetUri;
4949

5050
/**
5151
*
@@ -246,7 +246,7 @@ public JwtConfigurer decoder(JwtDecoder decoder) {
246246
}
247247

248248
public JwtConfigurer jwkSetUri(String uri) {
249-
this.decoder = new NimbusJwtDecoder(withJwkSetUri(uri).build());
249+
this.decoder = withJwkSetUri(uri).build();
250250
return this;
251251
}
252252

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurerTests.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import java.util.stream.Collectors;
3434
import javax.annotation.PreDestroy;
3535

36-
import com.nimbusds.jose.proc.SecurityContext;
37-
import com.nimbusds.jwt.proc.JWTProcessor;
3836
import okhttp3.mockwebserver.MockResponse;
3937
import okhttp3.mockwebserver.MockWebServer;
4038
import org.hamcrest.core.AllOf;
@@ -85,7 +83,6 @@
8583
import org.springframework.security.oauth2.jwt.JwtClaimNames;
8684
import org.springframework.security.oauth2.jwt.JwtDecoder;
8785
import org.springframework.security.oauth2.jwt.JwtException;
88-
import org.springframework.security.oauth2.jwt.JwtProcessors;
8986
import org.springframework.security.oauth2.jwt.JwtTimestampValidator;
9087
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
9188
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
@@ -122,7 +119,8 @@
122119
import static org.mockito.Mockito.mock;
123120
import static org.mockito.Mockito.verify;
124121
import static org.mockito.Mockito.when;
125-
import static org.springframework.security.oauth2.jwt.JwtProcessors.withPublicKey;
122+
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withJwkSetUri;
123+
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withPublicKey;
126124
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
127125
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
128126
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -1626,7 +1624,7 @@ protected void configure(HttpSecurity http) throws Exception {
16261624
JwtDecoder decoder() throws Exception {
16271625
RSAPublicKey publicKey = (RSAPublicKey)
16281626
KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(this.spec));
1629-
return new NimbusJwtDecoder(withPublicKey(publicKey).build());
1627+
return withPublicKey(publicKey).build();
16301628
}
16311629
}
16321630

@@ -1739,10 +1737,8 @@ RestOperations rest() {
17391737

17401738
@Bean
17411739
NimbusJwtDecoder jwtDecoder() {
1742-
JWTProcessor<SecurityContext> jwtProcessor =
1743-
JwtProcessors.withJwkSetUri("https://example.org/.well-known/jwks.json")
1744-
.restOperations(this.rest).build();
1745-
return new NimbusJwtDecoder(jwtProcessor);
1740+
return withJwkSetUri("https://example.org/.well-known/jwks.json")
1741+
.restOperations(this.rest).build();
17461742
}
17471743
}
17481744

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcIdTokenDecoderFactory.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.springframework.security.oauth2.client.oidc.authentication;
1717

18+
import java.util.Map;
19+
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.function.Function;
21+
1822
import org.springframework.security.oauth2.client.registration.ClientRegistration;
1923
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2024
import org.springframework.security.oauth2.core.OAuth2Error;
@@ -27,11 +31,7 @@
2731
import org.springframework.util.Assert;
2832
import org.springframework.util.StringUtils;
2933

30-
import java.util.Map;
31-
import java.util.concurrent.ConcurrentHashMap;
32-
import java.util.function.Function;
33-
34-
import static org.springframework.security.oauth2.jwt.JwtProcessors.withJwkSetUri;
34+
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withJwkSetUri;
3535

3636
/**
3737
* A {@link JwtDecoderFactory factory} that provides a {@link JwtDecoder}
@@ -65,7 +65,7 @@ public JwtDecoder createDecoder(ClientRegistration clientRegistration) {
6565
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
6666
}
6767
String jwkSetUri = clientRegistration.getProviderDetails().getJwkSetUri();
68-
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(withJwkSetUri(jwkSetUri).build());
68+
NimbusJwtDecoder jwtDecoder = withJwkSetUri(jwkSetUri).build();
6969
OAuth2TokenValidator<Jwt> jwtValidator = this.jwtValidatorFactory.apply(clientRegistration);
7070
jwtDecoder.setJwtValidator(jwtValidator);
7171
return jwtDecoder;

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtDecoders.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import org.springframework.web.client.RestTemplate;
2525
import org.springframework.web.util.UriComponentsBuilder;
2626

27-
import static org.springframework.security.oauth2.jwt.JwtProcessors.withJwkSetUri;
27+
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withJwkSetUri;
2828

2929
/**
3030
* Allows creating a {@link JwtDecoder} from an
@@ -60,8 +60,7 @@ public static JwtDecoder fromOidcIssuerLocation(String oidcIssuerLocation) {
6060
OAuth2TokenValidator<Jwt> jwtValidator =
6161
JwtValidators.createDefaultWithIssuer(oidcIssuerLocation);
6262

63-
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(
64-
withJwkSetUri(openidConfiguration.get("jwks_uri").toString()).build());
63+
NimbusJwtDecoder jwtDecoder = withJwkSetUri(openidConfiguration.get("jwks_uri").toString()).build();
6564
jwtDecoder.setJwtValidator(jwtValidator);
6665

6766
return jwtDecoder;

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtProcessors.java

-238
This file was deleted.

0 commit comments

Comments
 (0)