Make Public Missing Authority AccessDeniedHandler
Issue gh-17934
This commit is contained in:
+41
-198
@@ -16,40 +16,26 @@
|
||||
|
||||
package org.springframework.security.config.annotation.web.configurers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||
import org.springframework.security.authorization.AuthorityAuthorizationDecision;
|
||||
import org.springframework.security.authorization.AuthorizationDeniedException;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
|
||||
import org.springframework.security.web.access.DelegatingMissingAuthorityAccessDeniedHandler;
|
||||
import org.springframework.security.web.access.ExceptionTranslationFilter;
|
||||
import org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler;
|
||||
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
|
||||
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
|
||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
||||
import org.springframework.security.web.savedrequest.NullRequestCache;
|
||||
import org.springframework.security.web.savedrequest.RequestCache;
|
||||
import org.springframework.security.web.util.ThrowableAnalyzer;
|
||||
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
|
||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adds exception handling for Spring Security related exceptions to an application. All
|
||||
@@ -94,7 +80,8 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
|
||||
private LinkedHashMap<RequestMatcher, AccessDeniedHandler> defaultDeniedHandlerMappings = new LinkedHashMap<>();
|
||||
|
||||
private Map<String, LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>> entryPoints = new LinkedHashMap<>();
|
||||
private final DelegatingMissingAuthorityAccessDeniedHandler.Builder missingAuthoritiesHandlerBuilder = DelegatingMissingAuthorityAccessDeniedHandler
|
||||
.builder();
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
@@ -146,6 +133,37 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default {@link AuthenticationEntryPoint} to be used which prefers being
|
||||
* invoked for the provided missing {@link GrantedAuthority}.
|
||||
* @param entryPoint the {@link AuthenticationEntryPoint} to use for the given
|
||||
* {@code authority}
|
||||
* @param authority the authority
|
||||
* @return the {@link ExceptionHandlingConfigurer} for further customizations
|
||||
* @since 7.0
|
||||
*/
|
||||
public ExceptionHandlingConfigurer<H> defaultAuthenticationEntryPointFor(AuthenticationEntryPoint entryPoint,
|
||||
String authority) {
|
||||
this.missingAuthoritiesHandlerBuilder.addEntryPointFor(entryPoint, authority);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default {@link AuthenticationEntryPoint} to be used which prefers being
|
||||
* invoked for the provided missing {@link GrantedAuthority}.
|
||||
* @param entryPoint a consumer of a
|
||||
* {@link DelegatingAuthenticationEntryPoint.Builder} to use for the given
|
||||
* {@code authority}
|
||||
* @param authority the authority
|
||||
* @return the {@link ExceptionHandlingConfigurer} for further customizations
|
||||
* @since 7.0
|
||||
*/
|
||||
public ExceptionHandlingConfigurer<H> defaultAuthenticationEntryPointFor(
|
||||
Consumer<DelegatingAuthenticationEntryPoint.Builder> entryPoint, String authority) {
|
||||
this.missingAuthoritiesHandlerBuilder.addEntryPointFor(entryPoint, authority);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link AuthenticationEntryPoint} to be used.
|
||||
*
|
||||
@@ -189,26 +207,6 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExceptionHandlingConfigurer<H> defaultAuthenticationEntryPointFor(AuthenticationEntryPoint entryPoint,
|
||||
RequestMatcher preferredMatcher, String authority) {
|
||||
this.defaultEntryPointMappings.put(preferredMatcher, entryPoint);
|
||||
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> byMatcher = this.entryPoints.get(authority);
|
||||
if (byMatcher == null) {
|
||||
byMatcher = new LinkedHashMap<>();
|
||||
}
|
||||
byMatcher.put(preferredMatcher, entryPoint);
|
||||
this.entryPoints.put(authority, byMatcher);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExceptionHandlingConfigurer<H> defaultAuthenticationEntryPointFor(AuthenticationEntryPoint entryPoint,
|
||||
String authority) {
|
||||
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> byMatcher = new LinkedHashMap<>();
|
||||
byMatcher.put(AnyRequestMatcher.INSTANCE, entryPoint);
|
||||
this.entryPoints.put(authority, byMatcher);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any explicitly configured {@link AuthenticationEntryPoint}
|
||||
* @return
|
||||
@@ -269,22 +267,10 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
|
||||
private AccessDeniedHandler createDefaultDeniedHandler(H http) {
|
||||
AccessDeniedHandler defaults = createDefaultAccessDeniedHandler(http);
|
||||
if (this.entryPoints.isEmpty()) {
|
||||
return defaults;
|
||||
}
|
||||
Map<String, AccessDeniedHandler> deniedHandlers = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>> entry : this.entryPoints
|
||||
.entrySet()) {
|
||||
AuthenticationEntryPoint entryPoint = entryPointFrom(entry.getValue());
|
||||
AuthenticationEntryPointAccessDeniedHandlerAdapter deniedHandler = new AuthenticationEntryPointAccessDeniedHandlerAdapter(
|
||||
entryPoint);
|
||||
RequestCache requestCache = http.getSharedObject(RequestCache.class);
|
||||
if (requestCache != null) {
|
||||
deniedHandler.setRequestCache(requestCache);
|
||||
}
|
||||
deniedHandlers.put(entry.getKey(), deniedHandler);
|
||||
}
|
||||
return new AuthenticationFactorDelegatingAccessDeniedHandler(deniedHandlers, defaults);
|
||||
DelegatingMissingAuthorityAccessDeniedHandler deniedHandler = this.missingAuthoritiesHandlerBuilder.build();
|
||||
deniedHandler.setRequestCache(getRequestCache(http));
|
||||
deniedHandler.setDefaultAccessDeniedHandler(defaults);
|
||||
return deniedHandler;
|
||||
}
|
||||
|
||||
private AccessDeniedHandler createDefaultAccessDeniedHandler(H http) {
|
||||
@@ -299,29 +285,10 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
}
|
||||
|
||||
private AuthenticationEntryPoint createDefaultEntryPoint(H http) {
|
||||
AuthenticationEntryPoint defaults = entryPointFrom(this.defaultEntryPointMappings);
|
||||
if (this.entryPoints.isEmpty()) {
|
||||
return defaults;
|
||||
}
|
||||
Map<String, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>> entry : this.entryPoints
|
||||
.entrySet()) {
|
||||
entryPoints.put(entry.getKey(), entryPointFrom(entry.getValue()));
|
||||
}
|
||||
return new AuthenticationFactorDelegatingAuthenticationEntryPoint(entryPoints, defaults);
|
||||
}
|
||||
|
||||
private AuthenticationEntryPoint entryPointFrom(
|
||||
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints) {
|
||||
if (entryPoints.isEmpty()) {
|
||||
if (this.defaultEntryPoint == null) {
|
||||
return new Http403ForbiddenEntryPoint();
|
||||
}
|
||||
if (entryPoints.size() == 1) {
|
||||
return entryPoints.values().iterator().next();
|
||||
}
|
||||
DelegatingAuthenticationEntryPoint entryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
|
||||
entryPoint.setDefaultEntryPoint(entryPoints.values().iterator().next());
|
||||
return entryPoint;
|
||||
return this.defaultEntryPoint.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,128 +307,4 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
return new HttpSessionRequestCache();
|
||||
}
|
||||
|
||||
private static final class AuthenticationFactorDelegatingAuthenticationEntryPoint
|
||||
implements AuthenticationEntryPoint {
|
||||
|
||||
private final ThrowableAnalyzer throwableAnalyzer = new ThrowableAnalyzer();
|
||||
|
||||
private final Map<String, AuthenticationEntryPoint> entryPoints;
|
||||
|
||||
private final AuthenticationEntryPoint defaults;
|
||||
|
||||
private AuthenticationFactorDelegatingAuthenticationEntryPoint(
|
||||
Map<String, AuthenticationEntryPoint> entryPoints, AuthenticationEntryPoint defaults) {
|
||||
this.entryPoints = new LinkedHashMap<>(entryPoints);
|
||||
this.defaults = defaults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex)
|
||||
throws IOException, ServletException {
|
||||
Collection<GrantedAuthority> authorization = authorizationRequest(ex);
|
||||
entryPoint(authorization).commence(request, response, ex);
|
||||
}
|
||||
|
||||
private AuthenticationEntryPoint entryPoint(Collection<GrantedAuthority> authorities) {
|
||||
if (authorities == null) {
|
||||
return this.defaults;
|
||||
}
|
||||
for (GrantedAuthority needed : authorities) {
|
||||
AuthenticationEntryPoint entryPoint = this.entryPoints.get(needed.getAuthority());
|
||||
if (entryPoint != null) {
|
||||
return entryPoint;
|
||||
}
|
||||
}
|
||||
return this.defaults;
|
||||
}
|
||||
|
||||
private Collection<GrantedAuthority> authorizationRequest(Exception ex) {
|
||||
Throwable[] chain = this.throwableAnalyzer.determineCauseChain(ex);
|
||||
AuthorizationDeniedException denied = (AuthorizationDeniedException) this.throwableAnalyzer
|
||||
.getFirstThrowableOfType(AuthorizationDeniedException.class, chain);
|
||||
if (denied == null) {
|
||||
return List.of();
|
||||
}
|
||||
if (!(denied.getAuthorizationResult() instanceof AuthorityAuthorizationDecision authorization)) {
|
||||
return List.of();
|
||||
}
|
||||
return authorization.getAuthorities();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final class AuthenticationEntryPointAccessDeniedHandlerAdapter implements AccessDeniedHandler {
|
||||
|
||||
private final AuthenticationEntryPoint entryPoint;
|
||||
|
||||
private RequestCache requestCache = new NullRequestCache();
|
||||
|
||||
private AuthenticationEntryPointAccessDeniedHandlerAdapter(AuthenticationEntryPoint entryPoint) {
|
||||
this.entryPoint = entryPoint;
|
||||
}
|
||||
|
||||
void setRequestCache(RequestCache requestCache) {
|
||||
Assert.notNull(requestCache, "requestCache cannot be null");
|
||||
this.requestCache = requestCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException denied)
|
||||
throws IOException, ServletException {
|
||||
AuthenticationException ex = new InsufficientAuthenticationException("access denied", denied);
|
||||
this.requestCache.saveRequest(request, response);
|
||||
this.entryPoint.commence(request, response, ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final class AuthenticationFactorDelegatingAccessDeniedHandler implements AccessDeniedHandler {
|
||||
|
||||
private final ThrowableAnalyzer throwableAnalyzer = new ThrowableAnalyzer();
|
||||
|
||||
private final Map<String, AccessDeniedHandler> deniedHandlers;
|
||||
|
||||
private final AccessDeniedHandler defaults;
|
||||
|
||||
private AuthenticationFactorDelegatingAccessDeniedHandler(Map<String, AccessDeniedHandler> deniedHandlers,
|
||||
AccessDeniedHandler defaults) {
|
||||
this.deniedHandlers = new LinkedHashMap<>(deniedHandlers);
|
||||
this.defaults = defaults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex)
|
||||
throws IOException, ServletException {
|
||||
Collection<GrantedAuthority> authorization = authorizationRequest(ex);
|
||||
deniedHandler(authorization).handle(request, response, ex);
|
||||
}
|
||||
|
||||
private AccessDeniedHandler deniedHandler(Collection<GrantedAuthority> authorities) {
|
||||
if (authorities == null) {
|
||||
return this.defaults;
|
||||
}
|
||||
for (GrantedAuthority needed : authorities) {
|
||||
AccessDeniedHandler deniedHandler = this.deniedHandlers.get(needed.getAuthority());
|
||||
if (deniedHandler != null) {
|
||||
return deniedHandler;
|
||||
}
|
||||
}
|
||||
return this.defaults;
|
||||
}
|
||||
|
||||
private Collection<GrantedAuthority> authorizationRequest(Exception ex) {
|
||||
Throwable[] chain = this.throwableAnalyzer.determineCauseChain(ex);
|
||||
AuthorizationDeniedException denied = (AuthorizationDeniedException) this.throwableAnalyzer
|
||||
.getFirstThrowableOfType(AuthorizationDeniedException.class, chain);
|
||||
if (denied == null) {
|
||||
return List.of();
|
||||
}
|
||||
if (!(denied.getAuthorizationResult() instanceof AuthorityAuthorizationDecision authorization)) {
|
||||
return List.of();
|
||||
}
|
||||
return authorization.getAuthorities();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-1
@@ -233,7 +233,10 @@ public final class FormLoginConfigurer<H extends HttpSecurityBuilder<H>> extends
|
||||
initDefaultLoginFilter(http);
|
||||
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(getAuthenticationEntryPoint(), "FACTOR_PASSWORD");
|
||||
AuthenticationEntryPoint entryPoint = getAuthenticationEntryPoint();
|
||||
RequestMatcher requestMatcher = getAuthenticationEntryPointMatcher(http);
|
||||
exceptions.defaultAuthenticationEntryPointFor((ep) -> ep.addEntryPointFor(entryPoint, requestMatcher),
|
||||
"FACTOR_PASSWORD");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -192,8 +192,10 @@ public final class HttpBasicConfigurer<B extends HttpSecurityBuilder<B>>
|
||||
if (exceptionHandling == null) {
|
||||
return;
|
||||
}
|
||||
exceptionHandling.defaultAuthenticationEntryPointFor(postProcess(this.authenticationEntryPoint),
|
||||
preferredMatcher);
|
||||
AuthenticationEntryPoint entryPoint = postProcess(this.authenticationEntryPoint);
|
||||
exceptionHandling.defaultAuthenticationEntryPointFor(entryPoint, preferredMatcher);
|
||||
exceptionHandling.defaultAuthenticationEntryPointFor((ep) -> ep.addEntryPointFor(entryPoint, preferredMatcher),
|
||||
"FACTOR_PASSWORD");
|
||||
}
|
||||
|
||||
private void registerDefaultLogoutSuccessHandler(B http, RequestMatcher preferredMatcher) {
|
||||
|
||||
+5
-2
@@ -27,12 +27,14 @@ import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.security.authentication.ProviderManager;
|
||||
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.security.web.access.intercept.AuthorizationFilter;
|
||||
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
||||
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
|
||||
import org.springframework.security.web.authentication.ui.DefaultResourcesFilter;
|
||||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
||||
import org.springframework.security.web.csrf.CsrfToken;
|
||||
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
|
||||
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRpEntity;
|
||||
import org.springframework.security.web.webauthn.authentication.PublicKeyCredentialRequestOptionsFilter;
|
||||
import org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationFilter;
|
||||
@@ -155,8 +157,9 @@ public class WebAuthnConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
public void init(H http) throws Exception {
|
||||
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(new LoginUrlAuthenticationEntryPoint("/login"),
|
||||
"FACTOR_WEBAUTHN");
|
||||
AuthenticationEntryPoint entryPoint = new LoginUrlAuthenticationEntryPoint("/login");
|
||||
exceptions.defaultAuthenticationEntryPointFor(
|
||||
(ep) -> ep.addEntryPointFor(entryPoint, AnyRequestMatcher.INSTANCE), "FACTOR_WEBAUTHN");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -184,8 +184,9 @@ public final class X509Configurer<H extends HttpSecurityBuilder<H>>
|
||||
.setSharedObject(AuthenticationEntryPoint.class, new Http403ForbiddenEntryPoint());
|
||||
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(new Http403ForbiddenEntryPoint(), AnyRequestMatcher.INSTANCE,
|
||||
"FACTOR_X509");
|
||||
AuthenticationEntryPoint forbidden = new Http403ForbiddenEntryPoint();
|
||||
exceptions.defaultAuthenticationEntryPointFor(
|
||||
(ep) -> ep.addEntryPointFor(forbidden, AnyRequestMatcher.INSTANCE), "FACTOR_X509");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-5
@@ -373,10 +373,6 @@ public final class OAuth2LoginConfigurer<B extends HttpSecurityBuilder<B>>
|
||||
http.authenticationProvider(new OidcAuthenticationRequestChecker());
|
||||
}
|
||||
this.initDefaultLoginFilter(http);
|
||||
ExceptionHandlingConfigurer<B> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(getAuthenticationEntryPoint(), "FACTOR_AUTHORIZATION_CODE");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -561,11 +557,18 @@ public final class OAuth2LoginConfigurer<B extends HttpSecurityBuilder<B>>
|
||||
RequestMatcher loginUrlMatcher = new AndRequestMatcher(notXRequestedWith,
|
||||
new NegatedRequestMatcher(defaultLoginPageMatcher), formLoginNotEnabled);
|
||||
// @formatter:off
|
||||
return DelegatingAuthenticationEntryPoint.builder()
|
||||
AuthenticationEntryPoint loginEntryPoint = DelegatingAuthenticationEntryPoint.builder()
|
||||
.addEntryPointFor(loginUrlEntryPoint, loginUrlMatcher)
|
||||
.defaultEntryPoint(getAuthenticationEntryPoint())
|
||||
.build();
|
||||
// @formatter:on
|
||||
ExceptionHandlingConfigurer<B> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
RequestMatcher requestMatcher = getAuthenticationEntryPointMatcher(http);
|
||||
exceptions.defaultAuthenticationEntryPointFor((ep) -> ep.addEntryPointFor(loginEntryPoint, requestMatcher),
|
||||
"FACTOR_AUTHORIZATION_CODE");
|
||||
}
|
||||
return loginEntryPoint;
|
||||
}
|
||||
|
||||
private RequestMatcher getFormLoginNotEnabledRequestMatcher(B http) {
|
||||
|
||||
+2
-4
@@ -259,10 +259,6 @@ public final class OAuth2ResourceServerConfigurer<H extends HttpSecurityBuilder<
|
||||
if (authenticationProvider != null) {
|
||||
http.authenticationProvider(authenticationProvider);
|
||||
}
|
||||
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(this.authenticationEntryPoint, "FACTOR_BEARER");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -331,6 +327,8 @@ public final class OAuth2ResourceServerConfigurer<H extends HttpSecurityBuilder<
|
||||
RequestMatcher preferredMatcher = new OrRequestMatcher(
|
||||
Arrays.asList(this.requestMatcher, X_REQUESTED_WITH, restNotHtmlMatcher, allMatcher));
|
||||
exceptionHandling.defaultAuthenticationEntryPointFor(this.authenticationEntryPoint, preferredMatcher);
|
||||
exceptionHandling.defaultAuthenticationEntryPointFor(
|
||||
(ep) -> ep.addEntryPointFor(this.authenticationEntryPoint, preferredMatcher), "FACTOR_BEARER");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-62
@@ -16,15 +16,10 @@
|
||||
|
||||
package org.springframework.security.config.annotation.web.configurers.ott;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -42,13 +37,8 @@ import org.springframework.security.config.annotation.web.configurers.AbstractAu
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.security.web.FormPostRedirectStrategy;
|
||||
import org.springframework.security.web.RedirectStrategy;
|
||||
import org.springframework.security.web.authentication.AuthenticationConverter;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
@@ -67,7 +57,6 @@ import org.springframework.security.web.csrf.CsrfToken;
|
||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* An {@link AbstractHttpConfigurer} for One-Time Token Login.
|
||||
@@ -149,9 +138,10 @@ public final class OneTimeTokenLoginConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
intiDefaultLoginFilter(http);
|
||||
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
AuthenticationEntryPoint entryPoint = new PostAuthenticationEntryPoint(
|
||||
this.tokenGeneratingUrl + "?username={u}", Map.of("u", Authentication::getName));
|
||||
exceptions.defaultAuthenticationEntryPointFor(entryPoint, "FACTOR_OTT");
|
||||
AuthenticationEntryPoint entryPoint = getAuthenticationEntryPoint();
|
||||
RequestMatcher requestMatcher = getAuthenticationEntryPointMatcher(http);
|
||||
exceptions.defaultAuthenticationEntryPointFor((ep) -> ep.addEntryPointFor(entryPoint, requestMatcher),
|
||||
"FACTOR_OTT");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,52 +400,4 @@ public final class OneTimeTokenLoginConfigurer<H extends HttpSecurityBuilder<H>>
|
||||
return this.context;
|
||||
}
|
||||
|
||||
private static final class PostAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
private final String entryPointUri;
|
||||
|
||||
private final Map<String, Function<Authentication, String>> params;
|
||||
|
||||
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
||||
.getContextHolderStrategy();
|
||||
|
||||
private RedirectStrategy redirectStrategy = new FormPostRedirectStrategy();
|
||||
|
||||
private PostAuthenticationEntryPoint(String entryPointUri,
|
||||
Map<String, Function<Authentication, String>> params) {
|
||||
this.entryPointUri = entryPointUri;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException authException) throws IOException, ServletException {
|
||||
Authentication authentication = getAuthentication(authException);
|
||||
Assert.notNull(authentication, "could not find authentication in order to perform post");
|
||||
Map<String, String> params = this.params.entrySet()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().apply(authentication)));
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(this.entryPointUri);
|
||||
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
||||
if (csrf != null) {
|
||||
builder.queryParam(csrf.getParameterName(), csrf.getToken());
|
||||
}
|
||||
String entryPointUrl = builder.build(false).expand(params).toUriString();
|
||||
this.redirectStrategy.sendRedirect(request, response, entryPointUrl);
|
||||
}
|
||||
|
||||
private Authentication getAuthentication(AuthenticationException authException) {
|
||||
Authentication authentication = authException.getAuthenticationRequest();
|
||||
if (authentication != null && authentication.isAuthenticated()) {
|
||||
return authentication;
|
||||
}
|
||||
authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.isAuthenticated()) {
|
||||
return authentication;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-5
@@ -305,10 +305,6 @@ public final class Saml2LoginConfigurer<B extends HttpSecurityBuilder<B>>
|
||||
if (this.authenticationManager == null) {
|
||||
registerDefaultAuthenticationProvider(http);
|
||||
}
|
||||
ExceptionHandlingConfigurer<B> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
exceptions.defaultAuthenticationEntryPointFor(getAuthenticationEntryPoint(), "FACTOR_SAML_RESPONSE");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,11 +344,18 @@ public final class Saml2LoginConfigurer<B extends HttpSecurityBuilder<B>>
|
||||
RequestMatcher loginUrlMatcher = new AndRequestMatcher(notXRequestedWith,
|
||||
new NegatedRequestMatcher(defaultLoginPageMatcher));
|
||||
// @formatter:off
|
||||
return DelegatingAuthenticationEntryPoint.builder()
|
||||
AuthenticationEntryPoint loginEntryPoint = DelegatingAuthenticationEntryPoint.builder()
|
||||
.addEntryPointFor(loginUrlEntryPoint, loginUrlMatcher)
|
||||
.defaultEntryPoint(getAuthenticationEntryPoint())
|
||||
.build();
|
||||
// @formatter:on
|
||||
ExceptionHandlingConfigurer<B> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
||||
if (exceptions != null) {
|
||||
RequestMatcher requestMatcher = getAuthenticationEntryPointMatcher(http);
|
||||
exceptions.defaultAuthenticationEntryPointFor((ep) -> ep.addEntryPointFor(loginEntryPoint, requestMatcher),
|
||||
"FACTOR_SAML_RESPONSE");
|
||||
}
|
||||
return loginEntryPoint;
|
||||
}
|
||||
|
||||
private void setAuthenticationRequestRepository(B http,
|
||||
|
||||
+4
-6
@@ -64,7 +64,6 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
@@ -79,7 +78,6 @@ import static org.springframework.security.test.web.servlet.request.SecurityMock
|
||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@@ -423,8 +421,8 @@ public class FormLoginConfigurerTests {
|
||||
.andExpect(redirectedUrl("http://localhost/login"));
|
||||
user = PasswordEncodedUser.withUserDetails(user).authorities("profile:read", "FACTOR_PASSWORD").build();
|
||||
this.mockMvc.perform(get("/profile").with(user(user)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("/ott/generate")));
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(redirectedUrl("http://localhost/login"));
|
||||
user = PasswordEncodedUser.withUserDetails(user)
|
||||
.authorities("profile:read", "FACTOR_PASSWORD", "FACTOR_OTT")
|
||||
.build();
|
||||
@@ -433,8 +431,8 @@ public class FormLoginConfigurerTests {
|
||||
|
||||
@Test
|
||||
void requestWhenUnauthenticatedX509ThenRequiresTwoSteps() throws Exception {
|
||||
this.spring.register(MfaDslX509Config.class, UserConfig.class, org.springframework.security.config.annotation.web.configurers.FormLoginConfigurerTests.BasicMfaController.class).autowire();
|
||||
this.mockMvc.perform(get("/profile")).andExpect(status().isForbidden());
|
||||
this.spring.register(MfaDslX509Config.class, UserConfig.class, BasicMfaController.class).autowire();
|
||||
this.mockMvc.perform(get("/profile")).andExpect(status().is3xxRedirection());
|
||||
this.mockMvc.perform(get("/profile").with(user(User.withUsername("rod").authorities("profile:read").build())))
|
||||
.andExpect(status().isForbidden());
|
||||
this.mockMvc.perform(get("/login")).andExpect(status().isOk());
|
||||
|
||||
Reference in New Issue
Block a user