Spring-boot读书笔记一Role Naming Convention

Role Naming Convention
Spring Security Role Convention:

Roles are typically UPPERCASE,Spring automatically adds "ROLE_" prefix internally,roles("USER") becomes ROLE_USER authority

What Happens Internally

  1. .roles("USER") // Becomes: ROLE_USER
  2. .roles("ADMIN") // Becomes: ROLE_ADMIN

Authorization Check
.antMatchers("/delete/**").hasRole("ADMIN") // Looks for ROLE_ADMIN
If you used lowercase:
.roles("user") // Becomes: ROLE_user (wrong!)
Then hasRole("ADMIN") wouldn't match ROLE_user.

Correct Usage

  • Username: Can be any case ("user", "User", "USER")
  • Role: Should be uppercase ("USER", "ADMIN")

Summary
The role name must match what you use in your authorization rules (hasRole("ADMIN"), etc.).

posted @ 2026-01-08 12:36  kkbln  阅读(3)  评论(0)    收藏  举报