An MVC matcher selects HTTP requests for Spring Security rules using path patterns aligned with Spring MVC mappings. Use it to apply different authorization restrictions to the same path by HTTP method, or match paths independently of method. section 8.2
Choose a pattern according to the endpoint shape:
- `/a` matches only `/a`; `/a/*` matches one additional pathname; `/a/**` matches multiple pathnames, including `/a/b/c`. section 8.2
- Use `requestMatchers(HttpMethod method, String... patterns)` when GET and POST requests on one path need different restrictions. section 8.2
- Use `requestMatchers(String... patterns)` when the restriction depends only on the path and should cover any HTTP method. section 8.2
- Match path variables with `/a/{param}` or constrain them with `/a/{param:regex}`. section 8.2
Authorize a path variable by value
For `@GetMapping("/product/{code}")`, configure `requestMatchers("/product/{code:^[0-9]*$}").permitAll().anyRequest().denyAll()`. A request for `/product/12345` is permitted, while `/product/1234a` is denied. section 8.2
HandlerMappingIntrospector i = new HandlerMappingIntrospector(); MvcRequestMatcher r = new MvcRequestMatcher(i, "/ciao"); c.ignoringRequestMatchers(r);
Construct an MvcRequestMatcher with a HandlerMappingIntrospector when a RequestMatcher object is needed, such as excluding a path from CSRF protection. section 9.3
