Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you implement role-based access control in Spring Boot?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJan 24, 2025
UpdatedNov 16, 2026
7 min read

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.

Table of Contents

  • How RBAC works in Spring Security
  • Adding the dependency
  • Modelling users and roles
  • Loading users with a UserDetailsService
  • Securing endpoints with a SecurityFilterChain
  • Method-level security with @PreAuthorize
  • Testing role rules
  • Frequently Asked Questions
    • What is the difference between hasRole and hasAuthority?
    • Why do I get 403 even though the user has the role?
    • Can I use RBAC with JWT tokens?
    • Should I use roles or permissions?
  • Summary

How RBAC works in Spring Security

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.

CheckMatches 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

Adding the dependency

Add the security starter. Maven:

xml

Gradle:

text

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.

Modelling users and roles

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.

java

Spring Data JPA gives you the lookup method:

java

Loading users with a UserDetailsService

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(...).

java

Never store plain-text passwords. Register a PasswordEncoder bean and hash passwords with it when users are created.

Securing endpoints with a SecurityFilterChain

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().

java

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.

Method-level security with @PreAuthorize

URL rules are coarse. To protect business logic, enable method security and annotate the methods that need guarding.

java
java

@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.

Testing role rules

Use spring-security-test to verify each rule with @WithMockUser.

java

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.

Frequently Asked Questions

What is the difference between hasRole and hasAuthority?

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.

Why do I get 403 even though the user has the role?

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.

Can I use RBAC with JWT tokens?

Yes. Map the token claims, for example roles or scope, to GrantedAuthority objects with a JwtAuthenticationConverter. The same hasRole and @PreAuthorize rules then apply.

Should I use roles or permissions?

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.

Summary

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.

Share