public Collection<GrantedAuthority> getGrantedAuthorities(
     DirContextOperations context, String username) {
   log.debug("ALL AUTHORITY ");
   Collection<GrantedAuthority> auth =
       userDetailsService.loadUserByUsername(username).getAuthorities();
   for (GrantedAuthority a : auth) {
     log.debug("authority = " + a.toString());
   }
   return auth;
 }
Пример #2
0
  private Set<String> authoritiesToRoles(Collection<GrantedAuthority> c) {
    Set<String> target = new HashSet<String>();

    for (GrantedAuthority authority : c) {
      if (null == authority.getAuthority()) {
        throw new IllegalArgumentException(
            "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process "
                + authority.toString());
      }

      target.add(authority.getAuthority());
    }

    return target;
  }
 private String getCurrentLogedUser() {
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
   String currentUserLogedInfo;
   if (auth != null) {
     currentUserLogedInfo = auth.getName(); // get logged in username
     String authoritiesStr = "["; // roles
     for (GrantedAuthority ga : auth.getAuthorities()) {
       authoritiesStr += dictionary.getValue(ga.toString()) + " ";
     }
     authoritiesStr += "]";
     currentUserLogedInfo += " " + authoritiesStr;
   } else {
     // is loggedout
     currentUserLogedInfo = dictionary.getValue("ROLE_ANONYMOUS");
   }
   return currentUserLogedInfo;
 }