Skip to content

Spring Security's PathPatternRequestMatcher.Builder is not auto-configured when using WebMvcTest and spring-boot-security-test #49854

@gbaso

Description

@gbaso

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

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions