get role assigned to a user inside spring controller code example
Example: get role assigned to a user inside spring controller
private boolean hasRole(String role) {
Collection authorities = (Collection)
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
boolean hasRole = false;
for (GrantedAuthority authority : authorities) {
hasRole = authority.getAuthority().equals(role);
if (hasRole) {
break;
}
}
return hasRole;
}