|
@@ -19,9 +19,6 @@ import java.net.URI;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
-import com.nimbusds.jose.jwk.source.JWKSource;
|
|
|
|
-import com.nimbusds.jose.proc.SecurityContext;
|
|
|
|
-
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
@@ -35,9 +32,6 @@ import org.springframework.security.oauth2.server.authorization.authentication.O
|
|
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationProvider;
|
|
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenRevocationAuthenticationProvider;
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
|
|
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
|
|
-import org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcClientRegistrationAuthenticationProvider;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.oidc.web.OidcClientRegistrationEndpointFilter;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.oidc.web.OidcProviderConfigurationEndpointFilter;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter;
|
|
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter;
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationServerMetadataEndpointFilter;
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationServerMetadataEndpointFilter;
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenIntrospectionEndpointFilter;
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenIntrospectionEndpointFilter;
|
|
@@ -62,15 +56,14 @@ import org.springframework.util.Assert;
|
|
* @see OAuth2ClientAuthenticationConfigurer
|
|
* @see OAuth2ClientAuthenticationConfigurer
|
|
* @see OAuth2AuthorizationEndpointConfigurer
|
|
* @see OAuth2AuthorizationEndpointConfigurer
|
|
* @see OAuth2TokenEndpointConfigurer
|
|
* @see OAuth2TokenEndpointConfigurer
|
|
|
|
+ * @see OidcConfigurer
|
|
* @see RegisteredClientRepository
|
|
* @see RegisteredClientRepository
|
|
* @see OAuth2AuthorizationService
|
|
* @see OAuth2AuthorizationService
|
|
* @see OAuth2AuthorizationConsentService
|
|
* @see OAuth2AuthorizationConsentService
|
|
* @see OAuth2TokenIntrospectionEndpointFilter
|
|
* @see OAuth2TokenIntrospectionEndpointFilter
|
|
* @see OAuth2TokenRevocationEndpointFilter
|
|
* @see OAuth2TokenRevocationEndpointFilter
|
|
* @see NimbusJwkSetEndpointFilter
|
|
* @see NimbusJwkSetEndpointFilter
|
|
- * @see OidcProviderConfigurationEndpointFilter
|
|
|
|
* @see OAuth2AuthorizationServerMetadataEndpointFilter
|
|
* @see OAuth2AuthorizationServerMetadataEndpointFilter
|
|
- * @see OidcClientRegistrationEndpointFilter
|
|
|
|
*/
|
|
*/
|
|
public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBuilder<B>>
|
|
public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBuilder<B>>
|
|
extends AbstractHttpConfigurer<OAuth2AuthorizationServerConfigurer<B>, B> {
|
|
extends AbstractHttpConfigurer<OAuth2AuthorizationServerConfigurer<B>, B> {
|
|
@@ -79,18 +72,15 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
private RequestMatcher tokenIntrospectionEndpointMatcher;
|
|
private RequestMatcher tokenIntrospectionEndpointMatcher;
|
|
private RequestMatcher tokenRevocationEndpointMatcher;
|
|
private RequestMatcher tokenRevocationEndpointMatcher;
|
|
private RequestMatcher jwkSetEndpointMatcher;
|
|
private RequestMatcher jwkSetEndpointMatcher;
|
|
- private RequestMatcher oidcProviderConfigurationEndpointMatcher;
|
|
|
|
private RequestMatcher authorizationServerMetadataEndpointMatcher;
|
|
private RequestMatcher authorizationServerMetadataEndpointMatcher;
|
|
- private RequestMatcher oidcClientRegistrationEndpointMatcher;
|
|
|
|
private final RequestMatcher endpointsMatcher = (request) ->
|
|
private final RequestMatcher endpointsMatcher = (request) ->
|
|
getRequestMatcher(OAuth2AuthorizationEndpointConfigurer.class).matches(request) ||
|
|
getRequestMatcher(OAuth2AuthorizationEndpointConfigurer.class).matches(request) ||
|
|
getRequestMatcher(OAuth2TokenEndpointConfigurer.class).matches(request) ||
|
|
getRequestMatcher(OAuth2TokenEndpointConfigurer.class).matches(request) ||
|
|
|
|
+ getRequestMatcher(OidcConfigurer.class).matches(request) ||
|
|
this.tokenIntrospectionEndpointMatcher.matches(request) ||
|
|
this.tokenIntrospectionEndpointMatcher.matches(request) ||
|
|
this.tokenRevocationEndpointMatcher.matches(request) ||
|
|
this.tokenRevocationEndpointMatcher.matches(request) ||
|
|
this.jwkSetEndpointMatcher.matches(request) ||
|
|
this.jwkSetEndpointMatcher.matches(request) ||
|
|
- this.oidcProviderConfigurationEndpointMatcher.matches(request) ||
|
|
|
|
- this.authorizationServerMetadataEndpointMatcher.matches(request) ||
|
|
|
|
- this.oidcClientRegistrationEndpointMatcher.matches(request);
|
|
|
|
|
|
+ this.authorizationServerMetadataEndpointMatcher.matches(request);
|
|
|
|
|
|
/**
|
|
/**
|
|
* Sets the repository of registered clients.
|
|
* Sets the repository of registered clients.
|
|
@@ -173,6 +163,17 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Configures OpenID Connect 1.0 support.
|
|
|
|
+ *
|
|
|
|
+ * @param oidcCustomizer the {@link Customizer} providing access to the {@link OidcConfigurer}
|
|
|
|
+ * @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
|
|
|
|
+ */
|
|
|
|
+ public OAuth2AuthorizationServerConfigurer<B> oidc(Customizer<OidcConfigurer> oidcCustomizer) {
|
|
|
|
+ oidcCustomizer.customize(getConfigurer(OidcConfigurer.class));
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Returns a {@link RequestMatcher} for the authorization server endpoints.
|
|
* Returns a {@link RequestMatcher} for the authorization server endpoints.
|
|
*
|
|
*
|
|
@@ -201,13 +202,6 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
OAuth2ConfigurerUtils.getAuthorizationService(builder));
|
|
OAuth2ConfigurerUtils.getAuthorizationService(builder));
|
|
builder.authenticationProvider(postProcess(tokenRevocationAuthenticationProvider));
|
|
builder.authenticationProvider(postProcess(tokenRevocationAuthenticationProvider));
|
|
|
|
|
|
- // TODO Make OpenID Client Registration an "opt-in" feature
|
|
|
|
- OidcClientRegistrationAuthenticationProvider oidcClientRegistrationAuthenticationProvider =
|
|
|
|
- new OidcClientRegistrationAuthenticationProvider(
|
|
|
|
- OAuth2ConfigurerUtils.getRegisteredClientRepository(builder),
|
|
|
|
- OAuth2ConfigurerUtils.getAuthorizationService(builder));
|
|
|
|
- builder.authenticationProvider(postProcess(oidcClientRegistrationAuthenticationProvider));
|
|
|
|
-
|
|
|
|
ExceptionHandlingConfigurer<B> exceptionHandling = builder.getConfigurer(ExceptionHandlingConfigurer.class);
|
|
ExceptionHandlingConfigurer<B> exceptionHandling = builder.getConfigurer(ExceptionHandlingConfigurer.class);
|
|
if (exceptionHandling != null) {
|
|
if (exceptionHandling != null) {
|
|
exceptionHandling.defaultAuthenticationEntryPointFor(
|
|
exceptionHandling.defaultAuthenticationEntryPointFor(
|
|
@@ -225,22 +219,6 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
this.configurers.values().forEach(configurer -> configurer.configure(builder));
|
|
this.configurers.values().forEach(configurer -> configurer.configure(builder));
|
|
|
|
|
|
ProviderSettings providerSettings = OAuth2ConfigurerUtils.getProviderSettings(builder);
|
|
ProviderSettings providerSettings = OAuth2ConfigurerUtils.getProviderSettings(builder);
|
|
- if (providerSettings.getIssuer() != null) {
|
|
|
|
- OidcProviderConfigurationEndpointFilter oidcProviderConfigurationEndpointFilter =
|
|
|
|
- new OidcProviderConfigurationEndpointFilter(providerSettings);
|
|
|
|
- builder.addFilterBefore(postProcess(oidcProviderConfigurationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
-
|
|
|
|
- OAuth2AuthorizationServerMetadataEndpointFilter authorizationServerMetadataEndpointFilter =
|
|
|
|
- new OAuth2AuthorizationServerMetadataEndpointFilter(providerSettings);
|
|
|
|
- builder.addFilterBefore(postProcess(authorizationServerMetadataEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- JWKSource<SecurityContext> jwkSource = OAuth2ConfigurerUtils.getJwkSource(builder);
|
|
|
|
- NimbusJwkSetEndpointFilter jwkSetEndpointFilter = new NimbusJwkSetEndpointFilter(
|
|
|
|
- jwkSource,
|
|
|
|
- providerSettings.getJwkSetEndpoint());
|
|
|
|
- builder.addFilterBefore(postProcess(jwkSetEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
-
|
|
|
|
AuthenticationManager authenticationManager = builder.getSharedObject(AuthenticationManager.class);
|
|
AuthenticationManager authenticationManager = builder.getSharedObject(AuthenticationManager.class);
|
|
|
|
|
|
OAuth2TokenIntrospectionEndpointFilter tokenIntrospectionEndpointFilter =
|
|
OAuth2TokenIntrospectionEndpointFilter tokenIntrospectionEndpointFilter =
|
|
@@ -253,14 +231,19 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
new OAuth2TokenRevocationEndpointFilter(
|
|
new OAuth2TokenRevocationEndpointFilter(
|
|
authenticationManager,
|
|
authenticationManager,
|
|
providerSettings.getTokenRevocationEndpoint());
|
|
providerSettings.getTokenRevocationEndpoint());
|
|
- builder.addFilterAfter(postProcess(tokenRevocationEndpointFilter), OAuth2TokenIntrospectionEndpointFilter.class);
|
|
|
|
|
|
+ builder.addFilterAfter(postProcess(tokenRevocationEndpointFilter), FilterSecurityInterceptor.class);
|
|
|
|
|
|
- // TODO Make OpenID Client Registration an "opt-in" feature
|
|
|
|
- OidcClientRegistrationEndpointFilter oidcClientRegistrationEndpointFilter =
|
|
|
|
- new OidcClientRegistrationEndpointFilter(
|
|
|
|
- authenticationManager,
|
|
|
|
- providerSettings.getOidcClientRegistrationEndpoint());
|
|
|
|
- builder.addFilterAfter(postProcess(oidcClientRegistrationEndpointFilter), OAuth2TokenRevocationEndpointFilter.class);
|
|
|
|
|
|
+ NimbusJwkSetEndpointFilter jwkSetEndpointFilter =
|
|
|
|
+ new NimbusJwkSetEndpointFilter(
|
|
|
|
+ OAuth2ConfigurerUtils.getJwkSource(builder),
|
|
|
|
+ providerSettings.getJwkSetEndpoint());
|
|
|
|
+ builder.addFilterBefore(postProcess(jwkSetEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
+
|
|
|
|
+ if (providerSettings.getIssuer() != null) {
|
|
|
|
+ OAuth2AuthorizationServerMetadataEndpointFilter authorizationServerMetadataEndpointFilter =
|
|
|
|
+ new OAuth2AuthorizationServerMetadataEndpointFilter(providerSettings);
|
|
|
|
+ builder.addFilterBefore(postProcess(authorizationServerMetadataEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer> createConfigurers() {
|
|
private Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer> createConfigurers() {
|
|
@@ -268,6 +251,7 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
configurers.put(OAuth2ClientAuthenticationConfigurer.class, new OAuth2ClientAuthenticationConfigurer(this::postProcess));
|
|
configurers.put(OAuth2ClientAuthenticationConfigurer.class, new OAuth2ClientAuthenticationConfigurer(this::postProcess));
|
|
configurers.put(OAuth2AuthorizationEndpointConfigurer.class, new OAuth2AuthorizationEndpointConfigurer(this::postProcess));
|
|
configurers.put(OAuth2AuthorizationEndpointConfigurer.class, new OAuth2AuthorizationEndpointConfigurer(this::postProcess));
|
|
configurers.put(OAuth2TokenEndpointConfigurer.class, new OAuth2TokenEndpointConfigurer(this::postProcess));
|
|
configurers.put(OAuth2TokenEndpointConfigurer.class, new OAuth2TokenEndpointConfigurer(this::postProcess));
|
|
|
|
+ configurers.put(OidcConfigurer.class, new OidcConfigurer(this::postProcess));
|
|
return configurers;
|
|
return configurers;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -287,12 +271,8 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
|
providerSettings.getTokenRevocationEndpoint(), HttpMethod.POST.name());
|
|
providerSettings.getTokenRevocationEndpoint(), HttpMethod.POST.name());
|
|
this.jwkSetEndpointMatcher = new AntPathRequestMatcher(
|
|
this.jwkSetEndpointMatcher = new AntPathRequestMatcher(
|
|
providerSettings.getJwkSetEndpoint(), HttpMethod.GET.name());
|
|
providerSettings.getJwkSetEndpoint(), HttpMethod.GET.name());
|
|
- this.oidcProviderConfigurationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
|
- "/.well-known/openid-configuration", HttpMethod.GET.name());
|
|
|
|
this.authorizationServerMetadataEndpointMatcher = new AntPathRequestMatcher(
|
|
this.authorizationServerMetadataEndpointMatcher = new AntPathRequestMatcher(
|
|
"/.well-known/oauth-authorization-server", HttpMethod.GET.name());
|
|
"/.well-known/oauth-authorization-server", HttpMethod.GET.name());
|
|
- this.oidcClientRegistrationEndpointMatcher = new AntPathRequestMatcher(
|
|
|
|
- providerSettings.getOidcClientRegistrationEndpoint(), HttpMethod.POST.name());
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private static void validateProviderSettings(ProviderSettings providerSettings) {
|
|
private static void validateProviderSettings(ProviderSettings providerSettings) {
|