Skip to content

Commit 079054b

Browse files
committed
use same request macther in configurer and filter
1 parent 3e59aba commit 079054b

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationServerConfigurer.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import com.nimbusds.jose.jwk.source.JWKSource;
25-
2624
import org.springframework.context.ApplicationListener;
2725
import org.springframework.context.event.GenericApplicationListenerAdapter;
2826
import org.springframework.context.event.SmartApplicationListener;
29-
import org.springframework.http.HttpMethod;
3027
import org.springframework.http.HttpStatus;
3128
import org.springframework.security.config.Customizer;
3229
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -53,11 +50,12 @@
5350
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
5451
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
5552
import org.springframework.security.web.context.SecurityContextHolderFilter;
56-
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
5753
import org.springframework.security.web.util.matcher.OrRequestMatcher;
5854
import org.springframework.security.web.util.matcher.RequestMatcher;
5955
import org.springframework.util.Assert;
6056

57+
import com.nimbusds.jose.jwk.source.JWKSource;
58+
6159
/**
6260
* An {@link AbstractHttpConfigurer} for OAuth 2.0 Authorization Server support.
6361
*
@@ -88,6 +86,7 @@ public final class OAuth2AuthorizationServerConfigurer
8886
private final Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer> configurers = createConfigurers();
8987
private RequestMatcher endpointsMatcher;
9088
private AuthorizationServerContextResolver authorizationServerContextResolver;
89+
private RequestMatcher jwkSetRequestMatcher;
9190

9291

9392
/**
@@ -328,8 +327,8 @@ public void init(HttpSecurity httpSecurity) {
328327
configurer.init(httpSecurity);
329328
requestMatchers.add(configurer.getRequestMatcher());
330329
});
331-
requestMatchers.add(new AntPathRequestMatcher(
332-
authorizationServerSettings.getJwkSetEndpoint(), HttpMethod.GET.name()));
330+
this.jwkSetRequestMatcher = NimbusJwkSetEndpointFilter.createDefaultRequestMatcher(authorizationServerSettings.getJwkSetEndpoint());
331+
requestMatchers.add(this.jwkSetRequestMatcher);
333332
this.endpointsMatcher = new OrRequestMatcher(requestMatchers);
334333

335334
ExceptionHandlingConfigurer<HttpSecurity> exceptionHandling = httpSecurity.getConfigurer(ExceptionHandlingConfigurer.class);
@@ -360,8 +359,8 @@ public void configure(HttpSecurity httpSecurity) {
360359

361360
JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource = OAuth2ConfigurerUtils.getJwkSource(httpSecurity);
362361
if (jwkSource != null) {
363-
NimbusJwkSetEndpointFilter jwkSetEndpointFilter = new NimbusJwkSetEndpointFilter(
364-
jwkSource, authorizationServerSettings.getJwkSetEndpoint());
362+
NimbusJwkSetEndpointFilter jwkSetEndpointFilter = new NimbusJwkSetEndpointFilter(jwkSource,
363+
this.jwkSetRequestMatcher);
365364
httpSecurity.addFilterBefore(postProcess(jwkSetEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
366365
}
367366
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/NimbusJwkSetEndpointFilter.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
import java.io.IOException;
1919
import java.io.Writer;
2020

21-
import jakarta.servlet.FilterChain;
22-
import jakarta.servlet.ServletException;
23-
import jakarta.servlet.http.HttpServletRequest;
24-
import jakarta.servlet.http.HttpServletResponse;
21+
import org.springframework.http.HttpMethod;
22+
import org.springframework.http.MediaType;
23+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
24+
import org.springframework.security.web.util.matcher.RequestMatcher;
25+
import org.springframework.util.Assert;
26+
import org.springframework.web.filter.OncePerRequestFilter;
2527

2628
import com.nimbusds.jose.jwk.JWKMatcher;
2729
import com.nimbusds.jose.jwk.JWKSelector;
2830
import com.nimbusds.jose.jwk.JWKSet;
2931
import com.nimbusds.jose.jwk.source.JWKSource;
3032
import com.nimbusds.jose.proc.SecurityContext;
3133

32-
import org.springframework.http.HttpMethod;
33-
import org.springframework.http.MediaType;
34-
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
35-
import org.springframework.security.web.util.matcher.RequestMatcher;
36-
import org.springframework.util.Assert;
37-
import org.springframework.web.filter.OncePerRequestFilter;
34+
import jakarta.servlet.FilterChain;
35+
import jakarta.servlet.ServletException;
36+
import jakarta.servlet.http.HttpServletRequest;
37+
import jakarta.servlet.http.HttpServletResponse;
3838

3939
/**
4040
* A {@code Filter} that processes JWK Set requests.
@@ -70,11 +70,27 @@ public NimbusJwkSetEndpointFilter(JWKSource<SecurityContext> jwkSource) {
7070
* @param jwkSetEndpointUri the endpoint {@code URI} for JWK Set requests
7171
*/
7272
public NimbusJwkSetEndpointFilter(JWKSource<SecurityContext> jwkSource, String jwkSetEndpointUri) {
73+
this(jwkSource, createDefaultRequestMatcher(jwkSetEndpointUri));
74+
}
75+
76+
/**
77+
* Constructs a {@code NimbusJwkSetEndpointFilter} using the provided parameters.
78+
*
79+
* @param jwkSource the {@code com.nimbusds.jose.jwk.source.JWKSource}
80+
* @param requestMatcher the endpoint matcher for JWK Set requests
81+
*/
82+
public NimbusJwkSetEndpointFilter(JWKSource<SecurityContext> jwkSource, RequestMatcher requestMatcher) {
7383
Assert.notNull(jwkSource, "jwkSource cannot be null");
74-
Assert.hasText(jwkSetEndpointUri, "jwkSetEndpointUri cannot be empty");
84+
Assert.notNull(requestMatcher, "requestMatcher cannot be null");
7585
this.jwkSource = jwkSource;
7686
this.jwkSelector = new JWKSelector(new JWKMatcher.Builder().build());
77-
this.requestMatcher = new AntPathRequestMatcher(jwkSetEndpointUri, HttpMethod.GET.name());
87+
this.requestMatcher = requestMatcher;
88+
}
89+
90+
public static RequestMatcher createDefaultRequestMatcher(String jwkSetEndpointUri) {
91+
Assert.hasText(jwkSetEndpointUri, "jwkSetEndpointUri cannot be empty");
92+
93+
return new AntPathRequestMatcher(jwkSetEndpointUri, HttpMethod.GET.name());
7894
}
7995

8096
@Override

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/web/NimbusJwkSetEndpointFilterTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.mock.web.MockHttpServletRequest;
3838
import org.springframework.mock.web.MockHttpServletResponse;
3939
import org.springframework.security.oauth2.jose.TestJwks;
40+
import org.springframework.security.web.util.matcher.RequestMatcher;
4041

4142
import static org.assertj.core.api.Assertions.assertThat;
4243
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -72,11 +73,18 @@ public void constructorWhenJwkSourceNullThenThrowIllegalArgumentException() {
7273

7374
@Test
7475
public void constructorWhenJwkSetEndpointUriNullThenThrowIllegalArgumentException() {
75-
assertThatThrownBy(() -> new NimbusJwkSetEndpointFilter(this.jwkSource, null))
76+
assertThatThrownBy(() -> new NimbusJwkSetEndpointFilter(this.jwkSource, (String) null))
7677
.isInstanceOf(IllegalArgumentException.class)
7778
.hasMessage("jwkSetEndpointUri cannot be empty");
7879
}
7980

81+
@Test
82+
public void constructorWhenJwkSetEndpointMatcherNullThenThrowIllegalArgumentException() {
83+
assertThatThrownBy(() -> new NimbusJwkSetEndpointFilter(this.jwkSource, (RequestMatcher) null))
84+
.isInstanceOf(IllegalArgumentException.class)
85+
.hasMessage("requestMatcher cannot be null");
86+
}
87+
8088
@Test
8189
public void doFilterWhenNotJwkSetRequestThenNotProcessed() throws Exception {
8290
String requestUri = "/path";

0 commit comments

Comments
 (0)