What does 'antMatchers' mean?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Spring Security, antMatchers referred to URL rules based on Ant-style path patterns such as "/admin/**". It was a convenient way to say which requests should be permitted, authenticated, or restricted to specific roles. The important modern detail is that antMatchers belongs to the older API shape; current Spring Security code usually uses requestMatchers instead.
What the ant Part Means
The word ant comes from Ant-style path matching. These patterns are simple wildcards for paths:
- '
?matches one character inside a path segment' - '
*matches zero or more characters inside one segment' - '
**matches across path segments'
So a pattern like "/css/**" matches files under /css/, while "/admin/*" matches one segment directly under /admin/.
That is the core idea behind antMatchers: use human-readable patterns instead of writing custom matching logic for every endpoint.
How antMatchers Was Used
In older Spring Security configurations, you often saw code like this:
This means:
- static assets are public
- '
/admin/paths require theADMINrole' - '
/profile/paths require a logged-in user' - everything else is denied
The rule order matters. Spring Security evaluates request matchers in order, so a broad rule placed too early can swallow a narrower rule that appears later.
What You Should Write in Current Spring Security
In modern Spring Security, the common API is requestMatchers under authorizeHttpRequests.
For simple path patterns, this reads almost the same. The difference is that the framework moved toward a more general request-matcher model instead of centering the configuration API on the older antMatchers method name.
In other words, antMatchers did not mean “special security logic.” It meant “apply authorization rules to requests matched with Ant-style path syntax.”
When Ant-Style Matching Is Enough
Ant-style patterns work well for common URL layouts:
- public static files under a directory
- admin or internal routes under a prefix
- method-specific endpoint rules combined with HTTP methods
Example with a specific HTTP method:
That gives you clear intent without introducing a custom matcher class.
When You Need Something More Specific
Sometimes Ant patterns are too broad. If a rule depends on a strict format such as alphanumeric usernames or a more complex path shape, use a regex matcher or another RequestMatcher implementation.
That is one reason the newer requestMatchers API is useful: it can accept more than one matching strategy.
Common Pitfalls
The most common mistake is assuming * and ** mean the same thing. They do not. * stays within one path segment, while ** can cross multiple segments.
Another mistake is putting a broad matcher before a narrow one. A rule like "/**".authenticated() placed too early can prevent later admin-specific rules from ever being reached.
A third issue is treating antMatchers as current terminology in brand-new projects. If you are using recent Spring Security versions, write requestMatchers and think in terms of request matcher types rather than memorizing an old method name.
Summary
- '
antMatcherswas the older Spring Security API for applying rules to Ant-style path patterns' - Ant-style patterns use
?,*, and**to match request paths - In current Spring Security,
requestMatchersis the usual replacement - Rule order matters because matchers are evaluated in sequence
- Use regex or custom matchers when Ant-style patterns are too broad for the route shape you need

