|
36 | 36 | import org.springframework.security.oauth2.jwt.JwtEncoder;
|
37 | 37 | import org.springframework.security.oauth2.jwt.NimbusJwsEncoder;
|
38 | 38 | import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService;
|
| 39 | +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationConsentService; |
39 | 40 | import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
|
40 | 41 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
41 | 42 | import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
|
| 43 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; |
42 | 44 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationProvider;
|
43 | 45 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationProvider;
|
44 | 46 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientCredentialsAuthenticationProvider;
|
@@ -107,6 +109,7 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
107 | 109 | this.oidcProviderConfigurationEndpointMatcher.matches(request) ||
|
108 | 110 | this.authorizationServerMetadataEndpointMatcher.matches(request) ||
|
109 | 111 | this.oidcClientRegistrationEndpointMatcher.matches(request);
|
| 112 | + private String consentPage = null; |
110 | 113 |
|
111 | 114 | /**
|
112 | 115 | * Sets the repository of registered clients.
|
@@ -144,6 +147,43 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
|
144 | 147 | return this;
|
145 | 148 | }
|
146 | 149 |
|
| 150 | + /** |
| 151 | + * Specify the URL to redirect {@code Resource Owners} to if consent is required during |
| 152 | + * the {@code authorization_code} flow. A default consent page will be generated when |
| 153 | + * this attribute is not specified. |
| 154 | + * |
| 155 | + * If a URL is specified, users are required to process the specified URL to generate |
| 156 | + * a consent page. The query string will contain the following parameters: |
| 157 | + * |
| 158 | + * <ul> |
| 159 | + * <li>{@code client_id} the client identifier</li> |
| 160 | + * <li>{@code scope} the space separated list of scopes present in the authorization request</li> |
| 161 | + * <li>{@code state} a CSRF protection token</li> |
| 162 | + * </ul> |
| 163 | + * |
| 164 | + * In general, the consent page should create a form that submits |
| 165 | + * a request with the following requirements: |
| 166 | + * |
| 167 | + * <ul> |
| 168 | + * <li>It must be an HTTP POST</li> |
| 169 | + * <li>It must be submitted to {@link ProviderSettings#authorizationEndpoint()}</li> |
| 170 | + * <li>It must include the received {@code client_id} as an HTTP parameter</li> |
| 171 | + * <li>It must include the received {@code state} as an HTTP parameter</li> |
| 172 | + * <li>It must include the list of {@code scope}s the {@code Resource Owners} |
| 173 | + * consents to as an HTTP parameter</li> |
| 174 | + * <li>It must include the {@code consent_action} parameter, with value either |
| 175 | + * {@code approve} or {@code cancel} as an HTTP parameter</li> |
| 176 | + * </ul> |
| 177 | + * |
| 178 | + * |
| 179 | + * @param consentPage the consent page to redirect to if consent is required (e.g. "/consent") |
| 180 | + * @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration |
| 181 | + */ |
| 182 | + public OAuth2AuthorizationServerConfigurer<B> consentPage(String consentPage) { |
| 183 | + this.consentPage = consentPage; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
147 | 187 | /**
|
148 | 188 | * Returns a {@link RequestMatcher} for the authorization server endpoints.
|
149 | 189 | *
|
@@ -263,7 +303,12 @@ public void configure(B builder) {
|
263 | 303 | new OAuth2AuthorizationEndpointFilter(
|
264 | 304 | getRegisteredClientRepository(builder),
|
265 | 305 | getAuthorizationService(builder),
|
266 |
| - providerSettings.authorizationEndpoint()); |
| 306 | + getAuthorizationConsentService(builder), |
| 307 | + providerSettings.authorizationEndpoint() |
| 308 | + ); |
| 309 | + if (this.consentPage != null) { |
| 310 | + authorizationEndpointFilter.setCustomUserConsentUri(this.consentPage); |
| 311 | + } |
267 | 312 | builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
268 | 313 |
|
269 | 314 | OAuth2TokenEndpointFilter tokenEndpointFilter =
|
@@ -347,6 +392,18 @@ private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationService get
|
347 | 392 | return authorizationService;
|
348 | 393 | }
|
349 | 394 |
|
| 395 | + private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationConsentService getAuthorizationConsentService(B builder) { |
| 396 | + OAuth2AuthorizationConsentService authorizationConsentService = builder.getSharedObject(OAuth2AuthorizationConsentService.class); |
| 397 | + if (authorizationConsentService == null) { |
| 398 | + authorizationConsentService = getOptionalBean(builder, OAuth2AuthorizationConsentService.class); |
| 399 | + if (authorizationConsentService == null) { |
| 400 | + authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService(); |
| 401 | + } |
| 402 | + builder.setSharedObject(OAuth2AuthorizationConsentService.class, authorizationConsentService); |
| 403 | + } |
| 404 | + return authorizationConsentService; |
| 405 | + } |
| 406 | + |
350 | 407 | private static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
|
351 | 408 | JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
|
352 | 409 | if (jwtEncoder == null) {
|
|
0 commit comments