📜  获取分配给弹簧控制器内部用户的角色 - 无论代码示例

📅  最后修改于: 2022-03-11 14:56:33.456000             🧑  作者: Mango

代码示例1
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;
}