To implement role-based access control (RBAC) in Spring Boot, add spring-boot-starter-security, load users with their roles from a UserDetailsService, and declare which roles may reach which endpoints in a SecurityFilterChain bean. For finer control, enable method security with @EnableMethodSecurity and annotate service or controller methods with @PreAuthorize.
The examples below use Spring Security 6 and later (Spring Boot 3.x and newer). The old WebSecurityConfigurerAdapter class was removed, so older tutorials that extend it no longer compile.
RBAC grants access based on the roles assigned to a user instead of on the user's identity. A user holds one or more roles such as USER or ADMIN, and each protected resource declares which roles may use it.
Spring Security represents every permission as a GrantedAuthority. A role is simply an authority whose name starts with the prefix ROLE_. That is why hasRole("ADMIN") checks for the authority ROLE_ADMIN, while hasAuthority("ROLE_ADMIN") checks the same thing without adding the prefix.
| Check | Matches authority |
|---|---|
hasRole("ADMIN") | ROLE_ADMIN |
hasAuthority("ROLE_ADMIN") | ROLE_ADMIN |
hasAuthority("report:read") | report:read (a fine-grained permission) |
hasAnyRole("ADMIN", "MANAGER") | ROLE_ADMIN or ROLE_MANAGER |
Add the security starter. Maven:
Gradle:
As soon as the starter is on the classpath, every endpoint requires authentication and Spring Boot generates a random password for a default user. You replace that behaviour with your own configuration in the next steps.
Store roles with the user. A simple model keeps the role names in a separate table and maps them as a collection of enum values or entities.
Spring Data JPA gives you the lookup method:
Spring Security authenticates through a UserDetailsService. Convert each role into a ROLE_-prefixed authority. The User.builder() helper does that for you when you call roles(...).
Never store plain-text passwords. Register a PasswordEncoder bean and hash passwords with it when users are created.
Declare URL rules in a SecurityFilterChain bean. Rules are evaluated in order, and the first match wins, so put specific patterns before general ones and end with anyRequest().
requestMatchers replaced the older antMatchers and mvcMatchers. A request that is not authenticated gets a 401 or a redirect to the login page, and an authenticated user without the required role gets a 403.
URL rules are coarse. To protect business logic, enable method security and annotate the methods that need guarding.
@EnableMethodSecurity replaces the deprecated @EnableGlobalMethodSecurity, and @PreAuthorize is enabled by default. The expression language lets you combine roles with method arguments, which URL rules cannot do. Prefer putting @PreAuthorize on service methods so the rule applies no matter which controller calls them.
Use spring-security-test to verify each rule with @WithMockUser.
Test both the allowed and the denied case for every protected route, so that a future change to the rules cannot silently open an endpoint.
hasRole("ADMIN") automatically adds the ROLE_ prefix and checks for ROLE_ADMIN. hasAuthority compares the exact string, so use it for permissions such as report:read that are not roles.
The most common cause is a stored role name that already includes ROLE_ while roles("ROLE_ADMIN") adds the prefix again, producing ROLE_ROLE_ADMIN. Also check rule order, because an earlier broad rule can match first.
Yes. Map the token claims, for example roles or scope, to GrantedAuthority objects with a JwtAuthenticationConverter. The same hasRole and @PreAuthorize rules then apply.
Start with roles. When roles become too coarse, give each role a set of permissions and check permissions with hasAuthority, so that code depends on what a user may do instead of who they are.
Add the security starter, load users and their roles through a UserDetailsService, and define URL rules in a SecurityFilterChain bean. Use @EnableMethodSecurity with @PreAuthorize for rules that depend on method arguments or business logic. Remember that hasRole adds the ROLE_ prefix, hash passwords with a PasswordEncoder, and test both allowed and denied requests.