|
35 | 35 | import org.springframework.security.oauth2.jwt.JwtEncoder;
|
36 | 36 | import org.springframework.security.oauth2.jwt.NimbusJwsEncoder;
|
37 | 37 | import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService;
|
| 38 | +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationConsentService; |
38 | 39 | import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
|
39 | 40 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
40 | 41 | import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
|
| 42 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; |
41 | 43 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationProvider;
|
42 | 44 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationProvider;
|
43 | 45 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientCredentialsAuthenticationProvider;
|
@@ -96,6 +98,7 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
96 | 98 | this.tokenRevocationEndpointMatcher.matches(request) ||
|
97 | 99 | this.jwkSetEndpointMatcher.matches(request) ||
|
98 | 100 | this.oidcProviderConfigurationEndpointMatcher.matches(request);
|
| 101 | + private String consentPage = null; |
99 | 102 |
|
100 | 103 | /**
|
101 | 104 | * Sets the repository of registered clients.
|
@@ -133,6 +136,43 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
|
133 | 136 | return this;
|
134 | 137 | }
|
135 | 138 |
|
| 139 | + /** |
| 140 | + * Specify the URL to redirect {@code Resource Owners} to if consent is required during |
| 141 | + * the {@code authorization_code} flow. A default consent page will be generated when |
| 142 | + * this attribute is not specified. |
| 143 | + * |
| 144 | + * If a URL is specified, users are required to process the specified URL to generate |
| 145 | + * a consent page. The query string will contain the following parameters: |
| 146 | + * |
| 147 | + * <ul> |
| 148 | + * <li>{@code client_id} the client identifier</li> |
| 149 | + * <li>{@code scope} the space separated list of scopes present in the authorization request</li> |
| 150 | + * <li>{@code state} a CSRF protection token</li> |
| 151 | + * </ul> |
| 152 | + * |
| 153 | + * In general, the consent page should create a form that submits |
| 154 | + * a request with the following requirements: |
| 155 | + * |
| 156 | + * <ul> |
| 157 | + * <li>It must be an HTTP POST</li> |
| 158 | + * <li>It must be submitted to {@link ProviderSettings#authorizationEndpoint()}</li> |
| 159 | + * <li>It must include the received {@code client_id} as an HTTP parameter</li> |
| 160 | + * <li>It must include the received {@code state} as an HTTP parameter</li> |
| 161 | + * <li>It must include the list of {@code scope}s the {@code Resource Owners} |
| 162 | + * consents to as an HTTP parameter</li> |
| 163 | + * <li>It must include the {@code consent_action} parameter, with value either |
| 164 | + * {@code approve} or {@code cancel} as an HTTP parameter</li> |
| 165 | + * </ul> |
| 166 | + * |
| 167 | + * |
| 168 | + * @param consentPage the consent page to redirect to if consent is required (e.g. "/consent") |
| 169 | + * @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration |
| 170 | + */ |
| 171 | + public OAuth2AuthorizationServerConfigurer<B> consentPage(String consentPage) { |
| 172 | + this.consentPage = consentPage; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
136 | 176 | /**
|
137 | 177 | * Returns a {@link RequestMatcher} for the authorization server endpoints.
|
138 | 178 | *
|
@@ -237,7 +277,12 @@ public void configure(B builder) {
|
237 | 277 | new OAuth2AuthorizationEndpointFilter(
|
238 | 278 | getRegisteredClientRepository(builder),
|
239 | 279 | getAuthorizationService(builder),
|
240 |
| - providerSettings.authorizationEndpoint()); |
| 280 | + getAuthorizationConsentService(builder), |
| 281 | + providerSettings.authorizationEndpoint() |
| 282 | + ); |
| 283 | + if (this.consentPage != null) { |
| 284 | + authorizationEndpointFilter.setCustomUserConsentUri(this.consentPage); |
| 285 | + } |
241 | 286 | builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
242 | 287 |
|
243 | 288 | OAuth2TokenEndpointFilter tokenEndpointFilter =
|
@@ -310,6 +355,18 @@ private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationService get
|
310 | 355 | return authorizationService;
|
311 | 356 | }
|
312 | 357 |
|
| 358 | + private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationConsentService getAuthorizationConsentService(B builder) { |
| 359 | + OAuth2AuthorizationConsentService authorizationConsentService = builder.getSharedObject(OAuth2AuthorizationConsentService.class); |
| 360 | + if (authorizationConsentService == null) { |
| 361 | + authorizationConsentService = getOptionalBean(builder, OAuth2AuthorizationConsentService.class); |
| 362 | + if (authorizationConsentService == null) { |
| 363 | + authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService(); |
| 364 | + } |
| 365 | + builder.setSharedObject(OAuth2AuthorizationConsentService.class, authorizationConsentService); |
| 366 | + } |
| 367 | + return authorizationConsentService; |
| 368 | + } |
| 369 | + |
313 | 370 | private static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
|
314 | 371 | JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
|
315 | 372 | if (jwtEncoder == null) {
|
|
0 commit comments