The Spring Security 7 migration guide suggests the use of PathPatternRequestMatcher.Builder to derive the full path of a matcher, including the servlet path prefix.
PathPatternRequestMatcher.Builder is provided by org.springframework.boot.security.autoconfigure.web.servlet.ServletWebSecurityAutoConfiguration, with base path obtained from DispatcherServletPath. In mock mvc tests, DispatcherServletPath is provided by org.springframework.boot.webmvc.test.autoconfigure.MockMvcAutoConfiguration, however PathPatternRequestMatcher.Builder is not created, due to missing DispatcherServletPath:
MockMvcAutoConfiguration#dispatcherServletPath matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.webmvc.autoconfigure.DispatcherServletPath; SearchStrategy: all) did not find any beans (OnBeanCondition)
ServletWebSecurityAutoConfiguration.PathPatternRequestMatcherBuilderConfiguration:
Did not match:
- @ConditionalOnBean (types: org.springframework.boot.webmvc.autoconfigure.DispatcherServletPath; SearchStrategy: all) did not find any beans of type org.springframework.boot.webmvc.autoconfigure.DispatcherServletPath (OnBeanCondition)
Matched:
- @ConditionalOnClass found required class 'org.springframework.boot.webmvc.autoconfigure.DispatcherServletPath' (OnClassCondition)
I suspect the auto-configuration for Spring Security is executing too early for the mock DispatcherServletPath to be picked up.
Sample repo: https://github.com/gbaso/spring-boot-pathpatternrequestmatcher
The Spring Security 7 migration guide suggests the use of
PathPatternRequestMatcher.Builderto derive the full path of a matcher, including the servlet path prefix.PathPatternRequestMatcher.Builderis provided byorg.springframework.boot.security.autoconfigure.web.servlet.ServletWebSecurityAutoConfiguration, with base path obtained fromDispatcherServletPath. In mock mvc tests,DispatcherServletPathis provided byorg.springframework.boot.webmvc.test.autoconfigure.MockMvcAutoConfiguration, howeverPathPatternRequestMatcher.Builderis not created, due to missingDispatcherServletPath:I suspect the auto-configuration for Spring Security is executing too early for the mock
DispatcherServletPathto be picked up.Sample repo: https://github.com/gbaso/spring-boot-pathpatternrequestmatcher