|
@@ -16,9 +16,7 @@
|
|
|
package org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization;
|
|
|
|
|
|
import java.net.URI;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import com.nimbusds.jose.jwk.source.JWKSource;
|
|
@@ -84,21 +82,18 @@ import org.springframework.util.StringUtils;
|
|
|
public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBuilder<B>>
|
|
|
extends AbstractHttpConfigurer<OAuth2AuthorizationServerConfigurer<B>, B> {
|
|
|
|
|
|
- private final RequestMatcher authorizationEndpointMatcher = new OrRequestMatcher(
|
|
|
- new AntPathRequestMatcher(
|
|
|
- OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI,
|
|
|
- HttpMethod.GET.name()),
|
|
|
- new AntPathRequestMatcher(
|
|
|
- OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI,
|
|
|
- HttpMethod.POST.name()));
|
|
|
- private final RequestMatcher tokenEndpointMatcher = new AntPathRequestMatcher(
|
|
|
- OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI, HttpMethod.POST.name());
|
|
|
- private final RequestMatcher tokenRevocationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
- OAuth2TokenRevocationEndpointFilter.DEFAULT_TOKEN_REVOCATION_ENDPOINT_URI, HttpMethod.POST.name());
|
|
|
- private final RequestMatcher jwkSetEndpointMatcher = new AntPathRequestMatcher(
|
|
|
- NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI, HttpMethod.GET.name());
|
|
|
- private final RequestMatcher oidcProviderConfigurationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
- OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI, HttpMethod.GET.name());
|
|
|
+ private RequestMatcher authorizationEndpointMatcher;
|
|
|
+ private RequestMatcher tokenEndpointMatcher;
|
|
|
+ private RequestMatcher tokenRevocationEndpointMatcher;
|
|
|
+ private RequestMatcher jwkSetEndpointMatcher;
|
|
|
+ private RequestMatcher oidcProviderConfigurationEndpointMatcher;
|
|
|
+ private final RequestMatcher endpointsMatcher = request -> {
|
|
|
+ return this.authorizationEndpointMatcher.matches(request) ||
|
|
|
+ this.tokenEndpointMatcher.matches(request) ||
|
|
|
+ this.tokenRevocationEndpointMatcher.matches(request) ||
|
|
|
+ this.jwkSetEndpointMatcher.matches(request) ||
|
|
|
+ this.oidcProviderConfigurationEndpointMatcher.matches(request);
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* Sets the repository of registered clients.
|
|
@@ -137,21 +132,19 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns a {@code List} of {@link RequestMatcher}'s for the authorization server endpoints.
|
|
|
+ * Returns a {@link RequestMatcher} for the authorization server endpoints.
|
|
|
*
|
|
|
- * @return a {@code List} of {@link RequestMatcher}'s for the authorization server endpoints
|
|
|
+ * @return a {@link RequestMatcher} for the authorization server endpoints
|
|
|
*/
|
|
|
- public List<RequestMatcher> getEndpointMatchers() {
|
|
|
- // TODO Initialize matchers using URI's from ProviderSettings
|
|
|
- return Arrays.asList(this.authorizationEndpointMatcher, this.tokenEndpointMatcher,
|
|
|
- this.tokenRevocationEndpointMatcher, this.jwkSetEndpointMatcher,
|
|
|
- this.oidcProviderConfigurationEndpointMatcher);
|
|
|
+ public RequestMatcher getEndpointsMatcher() {
|
|
|
+ return this.endpointsMatcher;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void init(B builder) {
|
|
|
ProviderSettings providerSettings = getProviderSettings(builder);
|
|
|
validateProviderSettings(providerSettings);
|
|
|
+ initEndpointMatchers(providerSettings);
|
|
|
|
|
|
OAuth2ClientAuthenticationProvider clientAuthenticationProvider =
|
|
|
new OAuth2ClientAuthenticationProvider(
|
|
@@ -188,7 +181,9 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
|
if (exceptionHandling != null) {
|
|
|
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
|
|
|
entryPoints.put(
|
|
|
- new OrRequestMatcher(this.tokenEndpointMatcher, this.tokenRevocationEndpointMatcher),
|
|
|
+ new OrRequestMatcher(
|
|
|
+ this.tokenEndpointMatcher,
|
|
|
+ this.tokenRevocationEndpointMatcher),
|
|
|
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
|
|
|
DelegatingAuthenticationEntryPoint authenticationEntryPoint =
|
|
|
new DelegatingAuthenticationEntryPoint(entryPoints);
|
|
@@ -222,7 +217,9 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
|
OAuth2ClientAuthenticationFilter clientAuthenticationFilter =
|
|
|
new OAuth2ClientAuthenticationFilter(
|
|
|
authenticationManager,
|
|
|
- new OrRequestMatcher(this.tokenEndpointMatcher, this.tokenRevocationEndpointMatcher));
|
|
|
+ new OrRequestMatcher(
|
|
|
+ this.tokenEndpointMatcher,
|
|
|
+ this.tokenRevocationEndpointMatcher));
|
|
|
builder.addFilterAfter(postProcess(clientAuthenticationFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
|
|
OAuth2AuthorizationEndpointFilter authorizationEndpointFilter =
|
|
@@ -255,6 +252,24 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void initEndpointMatchers(ProviderSettings providerSettings) {
|
|
|
+ this.authorizationEndpointMatcher = new OrRequestMatcher(
|
|
|
+ new AntPathRequestMatcher(
|
|
|
+ providerSettings.authorizationEndpoint(),
|
|
|
+ HttpMethod.GET.name()),
|
|
|
+ new AntPathRequestMatcher(
|
|
|
+ providerSettings.authorizationEndpoint(),
|
|
|
+ HttpMethod.POST.name()));
|
|
|
+ this.tokenEndpointMatcher = new AntPathRequestMatcher(
|
|
|
+ providerSettings.tokenEndpoint(), HttpMethod.POST.name());
|
|
|
+ this.tokenRevocationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
+ providerSettings.tokenRevocationEndpoint(), HttpMethod.POST.name());
|
|
|
+ this.jwkSetEndpointMatcher = new AntPathRequestMatcher(
|
|
|
+ providerSettings.jwkSetEndpoint(), HttpMethod.GET.name());
|
|
|
+ this.oidcProviderConfigurationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
+ OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI, HttpMethod.GET.name());
|
|
|
+ }
|
|
|
+
|
|
|
private static <B extends HttpSecurityBuilder<B>> RegisteredClientRepository getRegisteredClientRepository(B builder) {
|
|
|
RegisteredClientRepository registeredClientRepository = builder.getSharedObject(RegisteredClientRepository.class);
|
|
|
if (registeredClientRepository == null) {
|