Skip to content

Commit 34b40de

Browse files
committed
Add By-RequestMatcher Exception Handling
Issue gh-5185
1 parent 98a2ca3 commit 34b40de

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

config/src/main/java/org/springframework/security/config/http/AuthenticationConfigBuilder.java

+28
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
import org.springframework.security.core.authority.mapping.SimpleMappableAttributesRetriever;
4747
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
4848
import org.springframework.security.web.access.ExceptionTranslationFilter;
49+
import org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler;
4950
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
51+
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
5052
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
5153
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
5254
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
@@ -165,6 +167,8 @@ final class AuthenticationConfigBuilder {
165167
private BeanDefinition authorizationCodeGrantFilter;
166168
private BeanReference authorizationCodeAuthenticationProviderRef;
167169

170+
private final Map<BeanDefinition, BeanMetadataElement> defaultDeniedHandlerMappings = new ManagedMap<>();
171+
private final Map<BeanDefinition, BeanMetadataElement> defaultEntryPointMappings = new ManagedMap<>();
168172
private final List<BeanDefinition> csrfIgnoreRequestMatchers = new ManagedList<>();
169173

170174
AuthenticationConfigBuilder(Element element, boolean forceAutoConfig,
@@ -806,13 +810,27 @@ private BeanMetadataElement createAccessDeniedHandler(Element element,
806810

807811
}
808812
accessDeniedHandler.addPropertyValue("errorPage", errorPage);
813+
return accessDeniedHandler.getBeanDefinition();
809814
}
810815
else if (StringUtils.hasText(ref)) {
811816
return new RuntimeBeanReference(ref);
812817
}
813818

814819
}
815820

821+
if (this.defaultDeniedHandlerMappings.isEmpty()) {
822+
return accessDeniedHandler.getBeanDefinition();
823+
}
824+
if (this.defaultDeniedHandlerMappings.size() == 1) {
825+
return this.defaultDeniedHandlerMappings.values().iterator().next();
826+
}
827+
828+
accessDeniedHandler = BeanDefinitionBuilder
829+
.rootBeanDefinition(RequestMatcherDelegatingAccessDeniedHandler.class);
830+
accessDeniedHandler.addConstructorArgValue(this.defaultDeniedHandlerMappings);
831+
accessDeniedHandler.addConstructorArgValue
832+
(BeanDefinitionBuilder.rootBeanDefinition(AccessDeniedHandlerImpl.class));
833+
816834
return accessDeniedHandler.getBeanDefinition();
817835
}
818836

@@ -825,6 +843,16 @@ private BeanMetadataElement selectEntryPoint() {
825843
return new RuntimeBeanReference(customEntryPoint);
826844
}
827845

846+
if (!defaultEntryPointMappings.isEmpty()) {
847+
if (defaultEntryPointMappings.size() == 1) {
848+
return defaultEntryPointMappings.values().iterator().next();
849+
}
850+
BeanDefinitionBuilder delegatingEntryPoint = BeanDefinitionBuilder
851+
.rootBeanDefinition(DelegatingAuthenticationEntryPoint.class);
852+
delegatingEntryPoint.addConstructorArgValue(defaultEntryPointMappings);
853+
return delegatingEntryPoint.getBeanDefinition();
854+
}
855+
828856
Element basicAuthElt = DomUtils.getChildElementByTagName(httpElt,
829857
Elements.BASIC_AUTH);
830858
Element formLoginElt = DomUtils.getChildElementByTagName(httpElt,

0 commit comments

Comments
 (0)