|
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;
|
@@ -100,6 +102,7 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
100 | 102 | this.jwkSetEndpointMatcher.matches(request) ||
|
101 | 103 | this.oidcProviderConfigurationEndpointMatcher.matches(request) ||
|
102 | 104 | this.authorizationServerMetadataEndpointMatcher.matches(request);
|
| 105 | + private String consentPage = null; |
103 | 106 |
|
104 | 107 | /**
|
105 | 108 | * Sets the repository of registered clients.
|
@@ -137,6 +140,43 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
|
137 | 140 | return this;
|
138 | 141 | }
|
139 | 142 |
|
| 143 | + /** |
| 144 | + * Specify the URL to redirect {@code Resource Owners} to if consent is required during |
| 145 | + * the {@code authorization_code} flow. A default consent page will be generated when |
| 146 | + * this attribute is not specified. |
| 147 | + * |
| 148 | + * If a URL is specified, users are required to process the specified URL to generate |
| 149 | + * a consent page. The query string will contain the following parameters: |
| 150 | + * |
| 151 | + * <ul> |
| 152 | + * <li>{@code client_id} the client identifier</li> |
| 153 | + * <li>{@code scope} the space separated list of scopes present in the authorization request</li> |
| 154 | + * <li>{@code state} a CSRF protection token</li> |
| 155 | + * </ul> |
| 156 | + * |
| 157 | + * In general, the consent page should create a form that submits |
| 158 | + * a request with the following requirements: |
| 159 | + * |
| 160 | + * <ul> |
| 161 | + * <li>It must be an HTTP POST</li> |
| 162 | + * <li>It must be submitted to {@link ProviderSettings#authorizationEndpoint()}</li> |
| 163 | + * <li>It must include the received {@code client_id} as an HTTP parameter</li> |
| 164 | + * <li>It must include the received {@code state} as an HTTP parameter</li> |
| 165 | + * <li>It must include the list of {@code scope}s the {@code Resource Owners} |
| 166 | + * consents to as an HTTP parameter</li> |
| 167 | + * <li>It must include the {@code consent_action} parameter, with value either |
| 168 | + * {@code approve} or {@code cancel} as an HTTP parameter</li> |
| 169 | + * </ul> |
| 170 | + * |
| 171 | + * |
| 172 | + * @param consentPage the consent page to redirect to if consent is required (e.g. "/consent") |
| 173 | + * @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration |
| 174 | + */ |
| 175 | + public OAuth2AuthorizationServerConfigurer<B> consentPage(String consentPage) { |
| 176 | + this.consentPage = consentPage; |
| 177 | + return this; |
| 178 | + } |
| 179 | + |
140 | 180 | /**
|
141 | 181 | * Returns a {@link RequestMatcher} for the authorization server endpoints.
|
142 | 182 | *
|
@@ -245,7 +285,12 @@ public void configure(B builder) {
|
245 | 285 | new OAuth2AuthorizationEndpointFilter(
|
246 | 286 | getRegisteredClientRepository(builder),
|
247 | 287 | getAuthorizationService(builder),
|
248 |
| - providerSettings.authorizationEndpoint()); |
| 288 | + getAuthorizationConsentService(builder), |
| 289 | + providerSettings.authorizationEndpoint() |
| 290 | + ); |
| 291 | + if (this.consentPage != null) { |
| 292 | + authorizationEndpointFilter.setCustomUserConsentUri(this.consentPage); |
| 293 | + } |
249 | 294 | builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
250 | 295 |
|
251 | 296 | OAuth2TokenEndpointFilter tokenEndpointFilter =
|
@@ -320,6 +365,18 @@ private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationService get
|
320 | 365 | return authorizationService;
|
321 | 366 | }
|
322 | 367 |
|
| 368 | + private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationConsentService getAuthorizationConsentService(B builder) { |
| 369 | + OAuth2AuthorizationConsentService authorizationConsentService = builder.getSharedObject(OAuth2AuthorizationConsentService.class); |
| 370 | + if (authorizationConsentService == null) { |
| 371 | + authorizationConsentService = getOptionalBean(builder, OAuth2AuthorizationConsentService.class); |
| 372 | + if (authorizationConsentService == null) { |
| 373 | + authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService(); |
| 374 | + } |
| 375 | + builder.setSharedObject(OAuth2AuthorizationConsentService.class, authorizationConsentService); |
| 376 | + } |
| 377 | + return authorizationConsentService; |
| 378 | + } |
| 379 | + |
323 | 380 | private static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
|
324 | 381 | JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
|
325 | 382 | if (jwtEncoder == null) {
|
|
0 commit comments