Spring Security hasRole() not working
Solution 1:
Try use hasAuthority
instead hasRole
inside HTML-tag.
sec:authorize="hasAuthority('ADMIN')"
Solution 2:
You are missing a concept:
- If you use
hasRole('ADMIN')
, in yourADMIN Enum
must beROLE_ADMIN
instead ofADMIN
. - If you use
hasAuthority('ADMIN')
, yourADMIN Enum
must beADMIN
.
In spring security, hasRole()
is the same as hasAuthority()
, but hasRole()
function map with Authority
without ROLE_
prefix.
You can find the accepted answer in this post: Difference between Role and GrantedAuthority in Spring Security
Solution 3:
I've had the same issue upgrading from Spring Security 3.x to 4.x. Changing hasRole()
to hasAuthority()
did the trick for me.
http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#el-common-built-in