Quick revision
The short answer
In Spring Security 6, requestMatchers("/orders/**") looks at the application: when Spring MVC is present it builds an MVC-aware matcher, so security matches paths the same way the DispatcherServlet maps them, and otherwise it falls back to an Ant-style matcher. Spring Security 7 uses PathPatternRequestMatcher by default; check your version. This matters because past bypasses came from security and MVC disagreeing about a path, such as /admin/ reaching a controller mapped to /admin, and Spring Framework 6 removed one of those disagreements by turning trailing-slash matching off by default.
What to remember
In this answer, we saw that URL authorization depends on two components reading the same path. Spring Security 6 picks an MVC-aware matcher for requestMatchers(String) when Spring MVC is present, and Spring Security 7 uses PathPatternRequestMatcher, the same parser MVC uses. We reproduced the old bypass where /admin/ skipped an Ant rule but reached the /admin controller, saw how Spring Framework 6 closed it by disabling trailing-slash matching, and ended with deny-by-default and method security as protection against the gaps that remain.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Spring Security 6, requestMatchers("/orders/**") looks at the application: when Spring MVC is present it builds an MVC-aware matcher, so security matches paths the same way the DispatcherServlet maps them, and otherwise it falls back to an Ant-style matcher. Spring Security 7 uses PathPatternRequestMatcher by default; check your version. This matters because past bypasses came from security and MVC disagreeing about a path, such as /admin/ reaching a controller mapped to /admin, and Spring Framework 6 removed one of those disagreements by turning trailing-slash matching off by default.
Why this question is asked
Authorization rules on URLs are only as good as the matcher behind them. If our security rule thinks a request is for one path while Spring MVC routes it to another, the rule is silently skipped. Interviewers ask this question to see whether we know that two separate components interpret the same URL, and why recent versions worked so hard to make them agree. In this answer, we will recap how request rules are matched, see how requestMatchers chooses a matcher in Spring Security 6 and 7, reproduce the classic trailing-slash bypass, see what Spring Framework 6 changed, and end with deny-by-default as the safety net.
1A Quick Recap: Who Looks at the URL?
A request to a Spring Boot web application passes two components that both read the path. First the Spring Security filter chain, a set of servlet filters that run before our code, decides whether the request is allowed. Its authorizeHttpRequests rules are a list of request matchers, each paired with a rule such as hasRole("ADMIN"). Then the DispatcherServlet, Spring MVC's front controller, picks the controller method whose @GetMapping or @RequestMapping matches the path.
These are two independent pieces of code. If the security matcher says /admin/ is not /admin, but MVC says it is, a request can skip the admin rule and still reach the admin controller. So the question is not only what a pattern means, but whether both components give it the same meaning.
2How requestMatchers(String) Chooses a Matcher
Spring Security 5 had separate methods, antMatchers() and mvcMatchers(), and developers often picked the wrong one. Spring Security 6 merged them into requestMatchers(String...) and made the choice itself: if Spring MVC is on the classpath, it creates an MvcRequestMatcher, which asks MVC's own path matching configuration whether the request matches; if not, it creates an AntPathRequestMatcher. In later 6.x releases it also refuses to guess when the application has more than one servlet, and asks us to say which servlet a pattern belongs to.
Spring Security 6.5 introduced PathPatternRequestMatcher, built on the same PathPattern parser that Spring MVC uses for its mappings, and Spring Security 7 makes it the default for string patterns, with the older Ant and MVC matchers deprecated or removed. The details moved between minor versions, so check your version, but the direction is always the same: security should parse paths exactly as MVC does.
| Version | Spring MVC present | No Spring MVC |
|---|---|---|
| Spring Security 5 | Your choice: antMatchers or mvcMatchers | antMatchers |
| Spring Security 6.0 to 6.4 | MvcRequestMatcher | AntPathRequestMatcher |
| Spring Security 6.5 | As 6.x, PathPatternRequestMatcher available | As 6.x |
| Spring Security 7 | PathPatternRequestMatcher | PathPatternRequestMatcher |
Notice that we write the same string in both versions. What changed is the object built from it, which is why the same configuration can behave slightly differently after an upgrade, and why upgrade notes deserve a careful read.
3Reproducing the Trailing-Slash Bypass
Here is the kind of configuration that caused real bypasses in the Spring Security 5 and Spring Framework 5 era, where MVC matched trailing slashes by default.
The reason is that the two matchers had different rules. The Ant matcher compared /admin/ with /admin and saw two different strings. MVC, with trailing-slash matching on, treated them as the same mapping. The request passed security as a public URL and reached a protected controller. mvcMatchers() existed precisely to avoid this, but it only helped teams that knew to use it.
4What Spring Framework 6 Changed
Spring Framework 6 deprecated trailing-slash matching and turned it off by default. Now a controller mapped to /admin answers only /admin, and /admin/ gets a 404 unless we map it explicitly. Together with Spring Security choosing an MVC-compatible matcher automatically, the example above can no longer be reproduced with default settings.
Notice that a pattern like /admin/** also protects /admin itself and anything below it, so it is safer than listing exact paths. If a team needs trailing slashes for old clients, the recommended approach is to redirect or rewrite them explicitly, for example with a filter or a proxy rule, rather than turning the old matching behaviour back on and reopening the gap.
5Deny by Default as the Safety Net
Matching bugs will not disappear completely. Encoded characters, path parameters, new servlets and proxies that rewrite URLs can all create new disagreements. So the final rule in every chain should limit the damage of a miss: anyRequest().authenticated() at least, or anyRequest().denyAll() in services where every endpoint is listed explicitly. Then a request that slips past a specific rule lands on a strict rule instead of on permitAll(). Spring Security's firewall also rejects suspicious paths such as those containing encoded slashes or path traversal sequences, and we should leave it at its strict default. Method security on sensitive services adds a second check that does not depend on URL parsing at all.
6Follow-up questions
Does /orders/** match /orders?
Yes. In both Ant-style and PathPattern syntax, a trailing /** matches zero or more path segments, so /orders, /orders/ (where the framework accepts it) and /orders/42/items all match. That is why /** patterns are the usual choice for protecting a whole area.
What should I do if my application registers more than one servlet?
Recent 6.x versions refuse to guess whether a String pattern is meant for the DispatcherServlet or another servlet and fail at startup with a message explaining the choice. We then build matchers explicitly, for example with an MvcRequestMatcher.Builder in 6.x or a PathPatternRequestMatcher builder with a servlet path in newer versions, so each rule says which servlet it belongs to.
Why not use regexMatchers for full control?
Regular expressions match the raw string, not the way MVC maps it, so they reintroduce exactly the disagreement we want to avoid. They are useful for odd legacy cases, but for normal controller paths the MVC-compatible matcher is safer.
7Conclusion
In this answer, we saw that URL authorization depends on two components reading the same path. Spring Security 6 picks an MVC-aware matcher for requestMatchers(String) when Spring MVC is present, and Spring Security 7 uses PathPatternRequestMatcher, the same parser MVC uses. We reproduced the old bypass where /admin/ skipped an Ant rule but reached the /admin controller, saw how Spring Framework 6 closed it by disabling trailing-slash matching, and ended with deny-by-default and method security as protection against the gaps that remain.
Open with
In Spring Security 6, requestMatchers with a String does not decide on its own syntax. If Spring MVC is on the classpath, it builds an MvcRequestMatcher, which uses MVC's own path matching, so security sees the path the same way the DispatcherServlet does; without MVC, it uses an Ant path matcher.
Then, if they want more
Spring Security 7 moves to PathPatternRequestMatcher by default, which is built on the same PathPattern parser as MVC mappings, though the exact rules shifted across minor versions, so I would check the version. The reason this matters is that most URL authorization bypasses came from the two sides disagreeing.
The classic case: an Ant rule protecting /admin, while MVC with trailing-slash matching mapped /admin/ to the same controller, so /admin/ passed security as an unprotected URL and still reached the admin page. Spring Framework 6 turned trailing-slash matching off by default, so /admin/ no longer maps to /admin, which removes that mismatch.
Even so, I always end the chain with anyRequest().authenticated() or denyAll(), so any request that slips past a specific rule meets a strict default rather than permitAll, and I put method security on sensitive operations so they do not depend on URL parsing at all.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6, Spring Framework 6, Spring Security 5, Spring Security 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In Spring Security 6 and later, how does requestMatchers("/orders/**") decide which kind...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/8cfe6fdcdc/