示例#1
0
  private Roles getRolesForSignedInUser() {
    // Retrieve the granted authorities from the current authentication. These correspond one on
    // one with user roles.
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (auth != null) {
      Roles roles = new Roles();

      Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();

      for (GrantedAuthority grantedAuthority : authorities) {
        roles.add(grantedAuthority.getAuthority());
      }

      if (roles.size() == 0) {
        LOGGER.warn("User " + auth.getPrincipal() + " logged in but no roles could be found!");
      }

      return roles;
    } else {
      LOGGER.warn("User is signed in but authentication is not set!");
      return null;
    }
  }