|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configurers; |
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Collection; |
19 | 21 | import java.util.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Function; |
| 24 | +import java.util.stream.Collectors; |
20 | 25 |
|
| 26 | +import jakarta.servlet.ServletException; |
| 27 | +import jakarta.servlet.http.HttpServletRequest; |
| 28 | +import jakarta.servlet.http.HttpServletResponse; |
| 29 | + |
| 30 | +import org.springframework.security.access.AccessDeniedException; |
| 31 | +import org.springframework.security.authentication.InsufficientAuthenticationException; |
| 32 | +import org.springframework.security.authorization.AuthorityAuthorizationDecision; |
| 33 | +import org.springframework.security.authorization.AuthorizationDeniedException; |
21 | 34 | import org.springframework.security.config.Customizer; |
22 | 35 | import org.springframework.security.config.annotation.web.HttpSecurityBuilder; |
23 | 36 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 37 | +import org.springframework.security.core.Authentication; |
| 38 | +import org.springframework.security.core.AuthenticationException; |
| 39 | +import org.springframework.security.core.GrantedAuthority; |
| 40 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 41 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
| 42 | +import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint; |
24 | 43 | import org.springframework.security.web.AuthenticationEntryPoint; |
| 44 | +import org.springframework.security.web.FormPostRedirectStrategy; |
| 45 | +import org.springframework.security.web.RedirectStrategy; |
25 | 46 | import org.springframework.security.web.access.AccessDeniedHandler; |
26 | 47 | import org.springframework.security.web.access.AccessDeniedHandlerImpl; |
27 | 48 | import org.springframework.security.web.access.ExceptionTranslationFilter; |
28 | 49 | import org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler; |
29 | 50 | import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint; |
30 | 51 | import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint; |
| 52 | +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; |
| 53 | +import org.springframework.security.web.authentication.ott.GenerateOneTimeTokenFilter; |
| 54 | +import org.springframework.security.web.csrf.CsrfToken; |
31 | 55 | import org.springframework.security.web.savedrequest.HttpSessionRequestCache; |
32 | 56 | import org.springframework.security.web.savedrequest.RequestCache; |
33 | 57 | import org.springframework.security.web.util.matcher.RequestMatcher; |
| 58 | +import org.springframework.util.Assert; |
| 59 | +import org.springframework.web.util.UriComponentsBuilder; |
34 | 60 |
|
35 | 61 | /** |
36 | 62 | * Adds exception handling for Spring Security related exceptions to an application. All |
@@ -225,13 +251,13 @@ AuthenticationEntryPoint getAuthenticationEntryPoint(H http) { |
225 | 251 |
|
226 | 252 | private AccessDeniedHandler createDefaultDeniedHandler(H http) { |
227 | 253 | if (this.defaultDeniedHandlerMappings.isEmpty()) { |
228 | | - return new AccessDeniedHandlerImpl(); |
| 254 | + return new AuthenticationFactorDelegatingAccessDeniedHandler(); |
229 | 255 | } |
230 | 256 | if (this.defaultDeniedHandlerMappings.size() == 1) { |
231 | 257 | return this.defaultDeniedHandlerMappings.values().iterator().next(); |
232 | 258 | } |
233 | 259 | return new RequestMatcherDelegatingAccessDeniedHandler(this.defaultDeniedHandlerMappings, |
234 | | - new AccessDeniedHandlerImpl()); |
| 260 | + new AuthenticationFactorDelegatingAccessDeniedHandler()); |
235 | 261 | } |
236 | 262 |
|
237 | 263 | private AuthenticationEntryPoint createDefaultEntryPoint(H http) { |
@@ -263,4 +289,96 @@ private RequestCache getRequestCache(H http) { |
263 | 289 | return new HttpSessionRequestCache(); |
264 | 290 | } |
265 | 291 |
|
| 292 | + private static final class AuthenticationFactorDelegatingAccessDeniedHandler implements AccessDeniedHandler { |
| 293 | + |
| 294 | + private final Map<String, AuthenticationEntryPoint> entryPoints = Map.of("FACTOR_PASSWORD", |
| 295 | + new LoginUrlAuthenticationEntryPoint("/login"), "FACTOR_AUTHORIZATION_CODE", |
| 296 | + new LoginUrlAuthenticationEntryPoint("/login"), "FACTOR_SAML_RESPONSE", |
| 297 | + new LoginUrlAuthenticationEntryPoint("/login"), "FACTOR_WEBAUTHN", |
| 298 | + new LoginUrlAuthenticationEntryPoint("/login"), "FACTOR_BEARER", |
| 299 | + new BearerTokenAuthenticationEntryPoint(), "FACTOR_OTT", |
| 300 | + new PostAuthenticationEntryPoint(GenerateOneTimeTokenFilter.DEFAULT_GENERATE_URL + "?username={u}", |
| 301 | + Map.of("u", Authentication::getName))); |
| 302 | + |
| 303 | + private final AccessDeniedHandler defaults = new AccessDeniedHandlerImpl(); |
| 304 | + |
| 305 | + @Override |
| 306 | + public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) |
| 307 | + throws IOException, ServletException { |
| 308 | + Collection<String> needed = authorizationRequest(ex); |
| 309 | + if (needed == null) { |
| 310 | + this.defaults.handle(request, response, ex); |
| 311 | + return; |
| 312 | + } |
| 313 | + for (String authority : needed) { |
| 314 | + AuthenticationEntryPoint entryPoint = this.entryPoints.get(authority); |
| 315 | + if (entryPoint != null) { |
| 316 | + AuthenticationException insufficient = new InsufficientAuthenticationException(ex.getMessage(), ex); |
| 317 | + entryPoint.commence(request, response, insufficient); |
| 318 | + return; |
| 319 | + } |
| 320 | + } |
| 321 | + this.defaults.handle(request, response, ex); |
| 322 | + } |
| 323 | + |
| 324 | + private Collection<String> authorizationRequest(AccessDeniedException access) { |
| 325 | + if (!(access instanceof AuthorizationDeniedException denied)) { |
| 326 | + return null; |
| 327 | + } |
| 328 | + if (!(denied.getAuthorizationResult() instanceof AuthorityAuthorizationDecision decision)) { |
| 329 | + return null; |
| 330 | + } |
| 331 | + return decision.getAuthorities().stream().map(GrantedAuthority::getAuthority).toList(); |
| 332 | + } |
| 333 | + |
| 334 | + } |
| 335 | + |
| 336 | + private static final class PostAuthenticationEntryPoint implements AuthenticationEntryPoint { |
| 337 | + |
| 338 | + private final String entryPointUri; |
| 339 | + |
| 340 | + private final Map<String, Function<Authentication, String>> params; |
| 341 | + |
| 342 | + private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder |
| 343 | + .getContextHolderStrategy(); |
| 344 | + |
| 345 | + private RedirectStrategy redirectStrategy = new FormPostRedirectStrategy(); |
| 346 | + |
| 347 | + private PostAuthenticationEntryPoint(String entryPointUri, |
| 348 | + Map<String, Function<Authentication, String>> params) { |
| 349 | + this.entryPointUri = entryPointUri; |
| 350 | + this.params = params; |
| 351 | + } |
| 352 | + |
| 353 | + @Override |
| 354 | + public void commence(HttpServletRequest request, HttpServletResponse response, |
| 355 | + AuthenticationException authException) throws IOException, ServletException { |
| 356 | + Authentication authentication = getAuthentication(authException); |
| 357 | + Assert.notNull(authentication, "could not find authentication in order to perform post"); |
| 358 | + Map<String, String> params = this.params.entrySet() |
| 359 | + .stream() |
| 360 | + .collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().apply(authentication))); |
| 361 | + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(this.entryPointUri); |
| 362 | + CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); |
| 363 | + if (csrf != null) { |
| 364 | + builder.queryParam(csrf.getParameterName(), csrf.getToken()); |
| 365 | + } |
| 366 | + String entryPointUrl = builder.build(false).expand(params).toUriString(); |
| 367 | + this.redirectStrategy.sendRedirect(request, response, entryPointUrl); |
| 368 | + } |
| 369 | + |
| 370 | + private Authentication getAuthentication(AuthenticationException authException) { |
| 371 | + Authentication authentication = authException.getAuthenticationRequest(); |
| 372 | + if (authentication != null && authentication.isAuthenticated()) { |
| 373 | + return authentication; |
| 374 | + } |
| 375 | + authentication = this.securityContextHolderStrategy.getContext().getAuthentication(); |
| 376 | + if (authentication != null && authentication.isAuthenticated()) { |
| 377 | + return authentication; |
| 378 | + } |
| 379 | + return null; |
| 380 | + } |
| 381 | + |
| 382 | + } |
| 383 | + |
266 | 384 | } |
0 commit comments