Example usage for org.springframework.security.core Authentication getAuthorities

List of usage examples for org.springframework.security.core Authentication getAuthorities

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getAuthorities.

Prototype

Collection<? extends GrantedAuthority> getAuthorities();

Source Link

Document

Set by an AuthenticationManager to indicate the authorities that the principal has been granted.

Usage

From source file:org.slc.sli.dashboard.util.SecurityUtil.java

public static boolean isAdmin() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(Constants.ROLE_IT_ADMINISTRATOR)) {
                    return true;
                }/*  w  ww . j ava2 s.co m*/
            }
        }
    }
    return false;
}

From source file:org.appverse.web.framework.backend.security.xs.SecurityHelper.java

/**
 * Retrieves the authorities list corresponding to the currently authenticated principal
 * @return the authorities granted to the principal
 *///from w  w w.j a  v a2  s .c  o  m
@SuppressWarnings("unchecked")
public static List<String> getAuthorities() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    List<String> credentials = new ArrayList<String>();
    Collection<GrantedAuthority> grantedAuthorities = (Collection<GrantedAuthority>) authentication
            .getAuthorities();
    for (GrantedAuthority grantedAuthority : grantedAuthorities) {
        credentials.add(grantedAuthority.getAuthority());
    }
    return credentials;
}

From source file:com.companyname.extension.PlatAuthentication.java

public static Authentication getPlatAuthentication(Authentication authentication) {

    if (authentication == null) {
        return null;
    }/*  w  w w. ja v a 2  s  . c om*/

    PlatAuthentication auth = new PlatAuthentication(authentication.getPrincipal(),
            authentication.getCredentials(), authentication.getAuthorities());

    BeanUtils.copyProperties(authentication, auth, new String[] { "authenticated" });

    return auth;
}

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

@SuppressWarnings("unchecked")
public static List<String> getAuthorities() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    List<String> credentials = new ArrayList<String>();
    Collection<GrantedAuthority> grantedAuthorities = (Collection<GrantedAuthority>) authentication
            .getAuthorities();/*from   w w  w.  j  a  v  a2s . c  om*/
    for (GrantedAuthority grantedAuthority : grantedAuthorities) {
        credentials.add(grantedAuthority.getAuthority());
    }
    return credentials;
}

From source file:net.maritimecloud.identityregistry.utils.AccessControlUtil.java

public static List<String> getMyRoles() {
    log.debug("Role lookup");
    List<String> roles = new ArrayList<>();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        for (GrantedAuthority authority : auth.getAuthorities()) {
            roles.add(authority.getAuthority());
        }//from w  ww. j a va  2 s  . c  o  m
    }
    return roles;
}

From source file:ru.org.linux.auth.AuthUtil.java

public static boolean hasAuthority(String authName) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return false;
    }// ww w .  j av  a  2  s.c om

    for (GrantedAuthority auth : authentication.getAuthorities()) {

        if (auth.getAuthority().equals(authName)) {
            return true;
        }
    }
    return false;
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ?, ??true./*from w  w  w  .ja va 2 s  .  c  o m*/
 */
public static boolean hasAnyRole(String... roles) {
    Authentication authentication = getAuthentication();

    if (authentication == null) {
        return false;
    }

    Collection<GrantedAuthority> grantedAuthorityList = authentication.getAuthorities();
    for (String role : roles) {
        for (GrantedAuthority authority : grantedAuthorityList) {
            if (role.equals(authority.getAuthority())) {
                return true;
            }
        }
    }

    return false;
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.security.impl.SecurityUtilImpl.java

/**
 * check if the user has administrator role
 * @return true if user is an administrator
 *//* w w w .  j  ava 2  s .  co  m*/
public static boolean isAdministrator() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        final Collection<GrantedAuthority> grantedAuthorities = authentication.getAuthorities();
        if (grantedAuthorities != null) {
            for (GrantedAuthority grantedAuthority : grantedAuthorities) {
                if ("ROLE_ANNOTATIONS_ADMINISTRATOR".equals(grantedAuthority.getAuthority())) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.github.javarch.jsf.tags.security.SpringSecurityELLibrary.java

private static GrantedAuthority[] getUserAuthorities() {
    if (SecurityContextHolder.getContext() == null) {
        System.out.println("security context is empty, this seems to be a bug/misconfiguration!");
        return new GrantedAuthority[0];
    }// w w w.  jav a 2s  .c o m
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null)
        return new GrantedAuthority[0];

    if (currentUser.getAuthorities() == null)
        return new GrantedAuthority[0];

    return currentUser.getAuthorities().toArray(new GrantedAuthority[] {});
}

From source file:org.malaguna.springsec.taglibs.SpringSecurityELLibrary.java

private static GrantedAuthority[] getUserAuthorities() {
    if (SecurityContextHolder.getContext() == null) {
        System.out.println("security context is empty, this seems to be a bug/misconfiguration!");
        return new GrantedAuthority[0];
    }//from ww  w  .  j  av a 2  s .  c  o  m
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null)
        return new GrantedAuthority[0];

    Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
    if (authorities == null)
        return new GrantedAuthority[0];

    return authorities.toArray(new GrantedAuthority[] {});
}