16
16
17
17
package org .springframework .boot .security .autoconfigure .servlet ;
18
18
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 ;
19
23
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
20
24
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
21
25
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
22
26
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication .Type ;
23
27
import org .springframework .boot .security .autoconfigure .ConditionalOnDefaultWebSecurity ;
24
28
import org .springframework .boot .security .autoconfigure .SecurityProperties ;
29
+ import org .springframework .boot .webmvc .autoconfigure .DispatcherServletPath ;
25
30
import org .springframework .context .annotation .Bean ;
26
31
import org .springframework .context .annotation .Configuration ;
27
32
import org .springframework .core .annotation .Order ;
30
35
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
31
36
import org .springframework .security .config .web .PathPatternRequestMatcherBuilderFactoryBean ;
32
37
import org .springframework .security .web .SecurityFilterChain ;
38
+ import org .springframework .security .web .servlet .util .matcher .PathPatternRequestMatcher ;
33
39
34
40
import static org .springframework .security .config .Customizer .withDefaults ;
35
41
@@ -48,6 +54,32 @@ PathPatternRequestMatcherBuilderFactoryBean workAroundSecurityDependencyCyclePat
48
54
return new PathPatternRequestMatcherBuilderFactoryBean ();
49
55
}
50
56
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
+
51
83
/**
52
84
* The default configuration for web security. It relies on Spring Security's
53
85
* content-negotiation strategy to determine what sort of authentication to use. If
0 commit comments