Spring Security - Securing Endpoints Using antMatchers()
Last Updated : 22 May, 2026
Spring Security provides security features for Java web applications by handling authentication and authorization. One of the commonly used methods in Spring Security 5 is antMatchers(), which helps secure endpoints based on roles, authorities, or authentication status.
Used to secure endpoints based on roles and authentication.
Supports wildcard URL pattern matching.
In Spring Security 6 and Spring Boot 3, antMatchers() has been removed and replaced with requestMatchers()
How antMatchers() Works
The mapping rules in antMatchers() support special characters for flexible matching
? : matches one character
* : matches zero or more characters
** : matches zero or more directories in a path
Examples:
org/g?g -> matches org/gfg, org/geg, etc.
org/*.jsp -> matches all .jsp files in the org directory
org/**/test.jsp -> matches all test.jsp files under the org path
Methods applied on antmatchers()
hasAnyRole(): Checks whether the authenticated user has a specific role to access the endpoint.
hasRole(): Allows access if the user has any one of the specified roles.
hasAuthority(): Checks whether the user has a specific authority or permission.
hasAnyAuthority(): Allows access if the user has any one of the specified authorities
authenticated(): Allows access only to authenticated (logged-in) users.
anonymous(): Allows access only to users who are not authenticated or logged in.
Implementation of Securing Endpoints Using antMatchers()
Step 1: Create Spring MVC Project and Configure Tomcat
Create a Dynamic Web Project in STS or Eclipse.
Configure the Apache Tomcat Server.
Before moving to the project letβs have a look at the complete project structure for our Spring MVC application.