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.duracloud.duradmin.util.SpaceUtil.java

protected static boolean hasRole(Authentication authentication, String role) {
    Collection authorities = authentication.getAuthorities();
    for (Object a : authorities) {
        if (((GrantedAuthority) a).getAuthority().equals(role)) {
            return true;
        }/*  www .  j a  va  2  s  . c  om*/
    }
    return false;
}

From source file:com.sonymobile.backlogtool.Util.java

/**
 * Checks if active user is logged in.// w ww.  ja  va 2s  . co  m
 * @return true if logged in, otherwise false
 */
public static boolean isLoggedIn() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        return false;
    }
    GrantedAuthority anonymous = new SimpleGrantedAuthority("ROLE_ANONYMOUS");
    return !auth.getAuthorities().contains(anonymous);
}

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

public static boolean hasAccessToOrg(String orgMrn) {
    if (orgMrn == null || orgMrn.trim().isEmpty()) {
        log.debug("The orgMrn was empty!");
        return false;
    }/*from   w ww  . j av a  2s. c o  m*/
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    // First check if the user is a SITE_ADMIN, in which case he gets access.
    for (GrantedAuthority authority : auth.getAuthorities()) {
        String role = authority.getAuthority();
        log.debug("User has role: " + role);
        if ("ROLE_SITE_ADMIN".equals(role)) {
            return true;
        }
    }
    log.debug("User not a SITE_ADMIN");
    // Check if the user is part of the organization
    if (auth instanceof KeycloakAuthenticationToken) {
        log.debug("OIDC authentication in process");
        // Keycloak authentication
        KeycloakAuthenticationToken kat = (KeycloakAuthenticationToken) auth;
        KeycloakSecurityContext ksc = (KeycloakSecurityContext) kat.getCredentials();
        Map<String, Object> otherClaims = ksc.getToken().getOtherClaims();
        if (otherClaims.containsKey(AccessControlUtil.ORG_PROPERTY_NAME)
                && ((String) otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME)).toLowerCase()
                        .equals(orgMrn.toLowerCase())) {
            log.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is in "
                    + orgMrn);
            return true;
        }
        log.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is not in "
                + orgMrn);
    } else if (auth instanceof PreAuthenticatedAuthenticationToken) {
        log.debug("Certificate authentication in process");
        // Certificate authentication
        PreAuthenticatedAuthenticationToken token = (PreAuthenticatedAuthenticationToken) auth;
        // Check that the Organization name of the accessed organization and the organization in the certificate is equal
        InetOrgPerson person = ((InetOrgPerson) token.getPrincipal());
        // The O(rganization) value in the certificate is an MRN
        String certOrgMrn = person.getO();
        if (orgMrn.equals(certOrgMrn)) {
            log.debug("Entity with O=" + certOrgMrn + " is in " + orgMrn);
            return true;
        }
        log.debug("Entity with O=" + certOrgMrn + " is not in " + orgMrn);
    } else {
        log.debug("Unknown authentication method: " + auth.getClass());
    }
    return false;
}

From source file:com.task.springsec.SecurityUtil.java

private static Collection<? extends GrantedAuthority> getPrincipalAuthorities() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (null == currentUser) {
        return Collections.emptyList();
    }/*from   w  w w  .  ja v a 2s.  c  o  m*/
    if ((null == currentUser.getAuthorities()) || (currentUser.getAuthorities().isEmpty())) {
        return Collections.emptyList();
    }
    Collection<? extends GrantedAuthority> granted = currentUser.getAuthorities();
    return granted;
}

From source file:br.com.suricattus.surispring.spring.security.util.SecurityUtil.java

/**
 * List user authorities./*w  w  w.j  a va2s.com*/
 *  
 * @return user authorities
 */
@SuppressWarnings("unchecked")
public static Set<String> getUserAuthorities() {
    if (SecurityContextHolder.getContext() == null)
        return Collections.EMPTY_SET;

    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null)
        return Collections.EMPTY_SET;

    Collection<? extends GrantedAuthority> grantedAauthorities = currentUser.getAuthorities();
    if (grantedAauthorities == null || grantedAauthorities.isEmpty())
        return Collections.EMPTY_SET;

    Set<String> authorities = new TreeSet<String>();
    for (GrantedAuthority ga : grantedAauthorities)
        authorities.add(ga.getAuthority());
    return authorities;
}

From source file:org.messic.server.facade.security.AuthenticationSessionManager.java

/**
 * Perform an authentication, returning the token associated
 * //from   w w  w. j a v  a2s  . c  om
 * @param authentication
 * @return
 */
public static String successfulAuthentication(Authentication authentication) {
    try {
        String token = UUID.randomUUID().toString();
        AuthenticationHolder authHolder = new AuthenticationHolder();
        for (GrantedAuthority gauth : authentication.getAuthorities()) {
            authHolder.authorities.add(gauth.getAuthority());
        }
        authHolder.auth = authentication;
        authMap.put(token, authHolder);
        return token;
    } catch (AuthenticationException e) {
        return null;
    }
}

From source file:org.messic.server.facade.security.AuthenticationSessionManager.java

/**
 * Perform an authentication, returning the token associated
 * /*from ww w  .ja v  a2s.c  o m*/
 * @param authentication
 * @return
 */
public static String successfulAuthenticationDLNA(Authentication authentication) {
    try {
        String token = UUID.randomUUID().toString();
        AuthenticationHolder authHolder = new AuthenticationHolder();
        for (GrantedAuthority gauth : authentication.getAuthorities()) {
            authHolder.authorities.add(gauth.getAuthority());
        }
        authHolder.auth = authentication;
        authMapDLNA.put(token, authHolder);
        return token;
    } catch (AuthenticationException e) {
        return null;
    }
}

From source file:mx.edu.um.mateo.general.utils.SpringSecurityUtils.java

/**
 * Get the current user's authorities./*  w w w. j  a v a2 s  . c  o m*/
 * 
 * @return a list of authorities (empty if not authenticated).
 */
public static Collection<GrantedAuthority> getPrincipalAuthorities() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return Collections.emptyList();
    }

    @SuppressWarnings("unchecked")
    Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) authentication.getAuthorities();
    if (authorities == null) {
        return Collections.emptyList();
    }

    // remove the fake role if it's there
    Collection<GrantedAuthority> copy = new ArrayList<>(authorities);
    for (Iterator<GrantedAuthority> iter = copy.iterator(); iter.hasNext();) {
        if (iter.next().getAuthority().equals(NO_ROLE)) {
            iter.remove();
        }
    }

    return copy;
}

From source file:org.zkoss.spring.security.SecurityUtil.java

private static Collection<? extends GrantedAuthority> getPrincipalAuthorities() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();

    if (null == currentUser) {
        return Collections.emptyList();
    }/*  ww  w.  ja  va2 s . com*/

    if ((null == currentUser.getAuthorities()) || (currentUser.getAuthorities().size() < 1)) {
        return Collections.emptyList();
    }

    Collection<? extends GrantedAuthority> granted = currentUser.getAuthorities();

    return granted;
}

From source file:net.cristcost.study.services.ServiceTestUtil.java

private static void dumpSecurityInformation(PrintWriter writer, AuthenticationManager authenticationManager) {
    writer.println("### General Security Information ###");

    writer.println("Security Strategy is " + SecurityContextHolder.getContextHolderStrategy().toString());

    writer.println("Current Thread is " + Thread.currentThread().getName() + " ("
            + Thread.currentThread().getId() + ")");

    writer.println();// ww w  .j av a 2  s  . c  o m

    if (authenticationManager != null) {
        writer.println("I've been injected with the AuthenticationManager");
    } else {
        writer.println("I've not been injected with the AuthenticationManager");
    }
    writer.println();

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        writer.println("There is an Authentication of type: " + authentication.getClass().getName());
        writer.println("Principal is of type: " + authentication.getPrincipal().getClass().getName()
                + " and is value is: " + authentication.getPrincipal().toString());
        for (GrantedAuthority ga : authentication.getAuthorities()) {
            writer.println(" - you have " + ga.getAuthority() + " authority");
        }

    } else {
        writer.println("There is no Authentication!");
    }
    writer.println();
}