Example usage for org.springframework.security.core GrantedAuthority getAuthority

List of usage examples for org.springframework.security.core GrantedAuthority getAuthority

Introduction

In this page you can find the example usage for org.springframework.security.core GrantedAuthority getAuthority.

Prototype

String getAuthority();

Source Link

Document

If the GrantedAuthority can be represented as a String and that String is sufficient in precision to be relied upon for an access control decision by an AccessDecisionManager (or delegate), this method should return such a String.

Usage

From source file:com.lll.util.SpringSecurityUtils.java

/**
 * ?, ??true.//from w  ww  . j av  a2  s.  co m
 */
public static boolean hasAnyRole(String[] roles) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Collection<GrantedAuthority> granteds = authentication.getAuthorities();
    for (String role : roles) {
        for (GrantedAuthority authority : granteds) {
            if (role.equals(authority.getAuthority())) {
                return true;
            }
        }
    }
    return false;
}

From source file:egovframework.let.utl.fcc.service.EgovAuthUtil.java

/**
 * @return true if the user has one of the specified roles.
 *///from   w ww. j  a  va 2 s  . c  o m
public static boolean hasRole(String[] roles) {
    boolean result = false;
    for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
        String userRole = authority.getAuthority();
        for (String role : roles) {
            if (role.equals(userRole)) {
                result = true;
                break;
            }
        }

        if (result) {
            break;
        }
    }

    return result;
}

From source file:org.red5.demo.auth.Application.java

public static boolean isAuthorized(String... roles) {
    // get the auth from the security context
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.isAuthenticated()) {
        UserDetails deets = (UserDetails) auth.getPrincipal();
        log.debug("enabled: {}", deets.isEnabled());
        Collection<GrantedAuthority> granted = deets.getAuthorities();
        for (GrantedAuthority authority : granted) {
            if (Arrays.asList(roles).contains(authority.getAuthority())) {
                log.debug("Authorized");
                return true;
            }/*from   w  w  w. j ava  2 s.  com*/
        }
    }
    log.debug("Not Authorized. User has not been granted any of: {}", roles);
    return false;
}

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;
    }/* w  w  w. j  a va2  s. c  o m*/

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

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

From source file:org.brutusin.rpc.RpcUtils.java

public static Set<String> getUserRoles(Object securityContext) {
    Set<String> roleSet = new TreeSet<String>();
    if (securityContext != null) {
        SecurityContext sc = (SecurityContext) securityContext;
        Collection<? extends GrantedAuthority> authorities = sc.getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            String auth = authority.getAuthority();
            if (auth.startsWith("ROLE_")) {
                auth = auth.substring(5);
            }// w  ww .j a v  a 2 s  . c o  m
            roleSet.add(auth);
        }
    }
    return Collections.unmodifiableSet(roleSet);
}

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

/**
 * ?, ??true./*from   w w  w. j av a 2  s.  c  om*/
 */
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:org.syncope.core.util.EntitlementUtil.java

public static Set<String> getOwnedEntitlementNames() {
    final Set<String> result = new HashSet<String>();

    final SecurityContext ctx = SecurityContextHolder.getContext();

    if (ctx != null && ctx.getAuthentication() != null && ctx.getAuthentication().getAuthorities() != null) {

        for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication()
                .getAuthorities()) {//from  w  w w  . ja va 2 s  . c o m

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

    return result;
}

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 w w .jav a 2s.  co m
    }
    return roles;
}

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
 *///from   w  ww . j a v  a2  s .  c  o  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

/**
 * Method that checks if the user holds <b>any</b> of the given roles.
 * Returns <code>true, when the first match is found, <code>false</code> if no match is found and
 * also <code>false</code> if no roles are given
 *
 * @param grantedRoles a comma seperated list of roles
 * @return true if any of the given roles are granted to the current user, false otherwise
 *//*from  ww w .  java2 s  . com*/
public static boolean ifAnyGranted(final String grantedRoles) {
    Set<String> parsedAuthorities = parseAuthorities(grantedRoles);
    if (parsedAuthorities.isEmpty())
        return false;

    GrantedAuthority[] authorities = getUserAuthorities();

    for (GrantedAuthority authority : authorities) {
        if (parsedAuthorities.contains(authority.getAuthority()))
            return true;
    }
    return false;
}