1616
1717package org .springframework .boot .security .autoconfigure .servlet ;
1818
19+ import org .springframework .beans .BeansException ;
20+ import org .springframework .beans .factory .ObjectProvider ;
21+ import org .springframework .beans .factory .config .BeanPostProcessor ;
22+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
1923import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
2024import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
2125import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
2226import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication .Type ;
2327import org .springframework .boot .security .autoconfigure .ConditionalOnDefaultWebSecurity ;
2428import org .springframework .boot .security .autoconfigure .SecurityProperties ;
29+ import org .springframework .boot .webmvc .autoconfigure .DispatcherServletPath ;
2530import org .springframework .context .annotation .Bean ;
2631import org .springframework .context .annotation .Configuration ;
2732import org .springframework .core .annotation .Order ;
3035import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
3136import org .springframework .security .config .web .PathPatternRequestMatcherBuilderFactoryBean ;
3237import org .springframework .security .web .SecurityFilterChain ;
38+ import org .springframework .security .web .servlet .util .matcher .PathPatternRequestMatcher ;
3339
3440import static org .springframework .security .config .Customizer .withDefaults ;
3541
@@ -48,6 +54,32 @@ PathPatternRequestMatcherBuilderFactoryBean workAroundSecurityDependencyCyclePat
4854 return new PathPatternRequestMatcherBuilderFactoryBean ();
4955 }
5056
57+ @ Configuration (proxyBeanMethods = false )
58+ @ ConditionalOnBean (DispatcherServletPath .class )
59+ @ ConditionalOnClass (DispatcherServletPath .class )
60+ static class PathPatternRequestMatcherBuilderConfiguration {
61+
62+ @ Bean
63+ static BeanPostProcessor pathPatternRequestMatcherBuilderBasePathCustomizer (
64+ ObjectProvider <DispatcherServletPath > dispatcherServletPath ) {
65+ return new BeanPostProcessor () {
66+
67+ @ Override
68+ public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
69+ if (bean instanceof PathPatternRequestMatcher .Builder builder ) {
70+ String path = dispatcherServletPath .getObject ().getPath ();
71+ if (!path .equals ("/" )) {
72+ return builder .basePath (path );
73+ }
74+ }
75+ return bean ;
76+ }
77+
78+ };
79+ }
80+
81+ }
82+
5183 /**
5284 * The default configuration for web security. It relies on Spring Security's
5385 * content-negotiation strategy to determine what sort of authentication to use. If
0 commit comments