Skip to content

Difference between @RolesAllowed and @PreAuthorize("hasAuthority")? #96

Answered by ch4mpy
kkhanhluu asked this question in Q&A
Discussion options

You must be logged in to vote

"Roles" in spring-security world are "authorities" with ROLE_ prefix.

Those 3 are the same:

  • @PreAuthorize("hasAuthority('ROLE_user')")
  • @PreAuthorize("hasRole('user')")
  • @RolesAllowed("user")

If the roles are not prefixed with ROLE_ in access-token, either:

  • use hasAuthority
  • configure the authorities converter to add ROLE_ prefix (this can be done from properties or yaml file if you use my starters and kept default authorities mapper).

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kkhanhluu
Comment options

Answer selected by kkhanhluu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants