|
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;
|
@@ -90,6 +92,7 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
|
90 | 92 | this.tokenRevocationEndpointMatcher.matches(request) ||
|
91 | 93 | this.jwkSetEndpointMatcher.matches(request) ||
|
92 | 94 | this.oidcProviderConfigurationEndpointMatcher.matches(request);
|
| 95 | + private String consentPage = null; |
93 | 96 |
|
94 | 97 | /**
|
95 | 98 | * Sets the repository of registered clients.
|
@@ -127,6 +130,43 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
|
127 | 130 | return this;
|
128 | 131 | }
|
129 | 132 |
|
| 133 | + /** |
| 134 | + * Specify the URL to redirect {@code Resource Owners} to if consent is required during |
| 135 | + * the {@code authorization_code} flow. A default consent page will be generated when |
| 136 | + * this attribute is not specified. |
| 137 | + * |
| 138 | + * If a URL is specified, users are required to process the specified URL to generate |
| 139 | + * a consent page. The query string will contain the following parameters: |
| 140 | + * |
| 141 | + * <ul> |
| 142 | + * <li>{@code client_id} the client identifier</li> |
| 143 | + * <li>{@code scope} the space separated list of scopes present in the authorization request</li> |
| 144 | + * <li>{@code state} a CSRF protection token</li> |
| 145 | + * </ul> |
| 146 | + * |
| 147 | + * In general, the consent page should create a form that submits |
| 148 | + * a request with the following requirements: |
| 149 | + * |
| 150 | + * <ul> |
| 151 | + * <li>It must be an HTTP POST</li> |
| 152 | + * <li>It must be submitted to {@link ProviderSettings#authorizationEndpoint()}</li> |
| 153 | + * <li>It must include the received {@code client_id} as an HTTP parameter</li> |
| 154 | + * <li>It must include the received {@code state} as an HTTP parameter</li> |
| 155 | + * <li>It must include the list of {@code scope}s the {@code Resource Owners} |
| 156 | + * consents to as an HTTP parameter</li> |
| 157 | + * <li>It must include the {@code consent_action} parameter, with value either |
| 158 | + * {@code approve} or {@code cancel} as an HTTP parameter</li> |
| 159 | + * </ul> |
| 160 | + * |
| 161 | + * |
| 162 | + * @param consentPage the consent page to redirect to if consent is required (e.g. "/consent") |
| 163 | + * @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration |
| 164 | + */ |
| 165 | + public OAuth2AuthorizationServerConfigurer<B> consentPage(String consentPage) { |
| 166 | + this.consentPage = consentPage; |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
130 | 170 | /**
|
131 | 171 | * Returns a {@link RequestMatcher} for the authorization server endpoints.
|
132 | 172 | *
|
@@ -223,7 +263,12 @@ public void configure(B builder) {
|
223 | 263 | new OAuth2AuthorizationEndpointFilter(
|
224 | 264 | getRegisteredClientRepository(builder),
|
225 | 265 | getAuthorizationService(builder),
|
226 |
| - providerSettings.authorizationEndpoint()); |
| 266 | + getAuthorizationConsentService(builder), |
| 267 | + providerSettings.authorizationEndpoint() |
| 268 | + ); |
| 269 | + if (this.consentPage != null) { |
| 270 | + authorizationEndpointFilter.setCustomUserConsentUri(this.consentPage); |
| 271 | + } |
227 | 272 | builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class);
|
228 | 273 |
|
229 | 274 | OAuth2TokenEndpointFilter tokenEndpointFilter =
|
@@ -288,6 +333,18 @@ private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationService get
|
288 | 333 | return authorizationService;
|
289 | 334 | }
|
290 | 335 |
|
| 336 | + private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationConsentService getAuthorizationConsentService(B builder) { |
| 337 | + OAuth2AuthorizationConsentService authorizationConsentService = builder.getSharedObject(OAuth2AuthorizationConsentService.class); |
| 338 | + if (authorizationConsentService == null) { |
| 339 | + authorizationConsentService = getOptionalBean(builder, OAuth2AuthorizationConsentService.class); |
| 340 | + if (authorizationConsentService == null) { |
| 341 | + authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService(); |
| 342 | + } |
| 343 | + builder.setSharedObject(OAuth2AuthorizationConsentService.class, authorizationConsentService); |
| 344 | + } |
| 345 | + return authorizationConsentService; |
| 346 | + } |
| 347 | + |
291 | 348 | private static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
|
292 | 349 | JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
|
293 | 350 | if (jwtEncoder == null) {
|
|
0 commit comments