Last updated: 2023-06-01

  1. Learn
  2. Spring Security
  3. Add Roles

How to add more roles in Spring Security

After setting up our application with Spring Security, we may want to customize the roles we use. For form-based login or JWT, users are usually loaded via an implementation of UserDetailsService - see for example here. How can we load roles from the database instead?

First version of our HttpUserDetailsService or JwtUserDetailsService

This initial version always assigns a single, fixed role to the user. If we want to read all roles from the database, we could use the following logic instead.

Reading and assigning more roles to our user

With this change our user already possesses all his new roles as a list of SimpleGrantedAuthority, coming directly from the database.

The Bootify Builder is a tool to create a modern Spring Boot application with custom database schema - directly in the browser. Spring Security for JWT or form-based is available in the Professional plan, including roles either from constants or a related role table of your schema.

Learn more
or see pricing