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.jamwiki.authentication.WikiUserDetailsImpl.java

/**
 * Utility method for converting a Spring Security <code>Authentication</code>
 * object into a <code>WikiUserDetailsImpl</code>.  If the user is logged-in then the
 * <code>Authentication</code> object will have the <code>WikiUserDetailsImpl</code>
 * as its principal.  If the user is not logged in then create an empty
 * <code>WikiUserDetailsImpl</code> object and assign it the same authorities as the
 * <code>Authentication</code> object.
 *
 * @param auth The Spring Security <code>Authentication</code> object that is being
 *  converted into a <code>WikiUserDetailsImpl</code> object.
 * @return Returns a <code>WikiUserDetailsImpl</code> object that corresponds to the
 *  Spring Security <code>Authentication</code> object.  If the user is not currently
 *  logged-in then an empty <code>WikiUserDetailsImpl</code> with the same authorities
 *  as the <code>Authentication</code> object is returned.  This method
 *  will never return <code>null</code>.
 * @throws AuthenticationCredentialsNotFoundException If authentication
 *  credentials are unavailable./* www .  ja  va  2 s.c  o m*/
 */
public static WikiUserDetailsImpl initWikiUserDetailsImpl(Authentication auth)
        throws AuthenticationCredentialsNotFoundException {
    if (auth == null) {
        throw new AuthenticationCredentialsNotFoundException("No authentication credential available");
    }
    if (auth instanceof AnonymousAuthenticationToken || !(auth.getPrincipal() instanceof UserDetails)) {
        // anonymous user
        return new WikiUserDetailsImpl(ANONYMOUS_USER_USERNAME, "", true, true, true, true,
                auth.getAuthorities());
    }
    // logged-in (or remembered) user
    if (auth.getPrincipal() instanceof WikiUserDetailsImpl) {
        return (WikiUserDetailsImpl) auth.getPrincipal();
    }
    return new WikiUserDetailsImpl((UserDetails) auth.getPrincipal());
}

From source file:grails.plugin.springsecurity.SpringSecurityUtils.java

/**
 * Get the username of the original user before switching to another.
 * @return the original login name//from ww w.j  av a2s .  c o m
 */
public static String getSwitchedUserOriginalUsername() {
    if (isSwitched()) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        for (GrantedAuthority auth : authentication.getAuthorities()) {
            if (auth instanceof SwitchUserGrantedAuthority) {
                return ((SwitchUserGrantedAuthority) auth).getSource().getName();
            }
        }
    }
    return null;
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils.java

/**
 * Get the current user's authorities./*w w w  .ja 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();
    }

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

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

    return copy;
}

From source file:edu.jhuapl.openessence.web.util.ControllerUtils.java

/**
 * Check if the specified user is authorized to access the given data source.
 *
 * @param authentication user's authentication
 *//*from w  w w .ja  va 2  s . com*/
public static boolean isUserAuthorized(Authentication authentication, JdbcOeDataSource ds) {
    Set<String> roles = ds.getRoles();
    if (roles == null || roles.isEmpty()) {
        return true;
    }

    for (GrantedAuthority eachAuthority : authentication.getAuthorities()) {
        if (roles.contains(eachAuthority.toString())) {
            return true;
        }
    }

    return false;
}

From source file:grails.plugin.springsecurity.SpringSecurityUtils.java

/**
 * Get the current user's authorities.//ww w .j  a v  a2s .  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();
    }

    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    if (authorities == null) {
        return Collections.emptyList();
    }

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

    return copy;
}

From source file:app.config.CustomSecurity.java

public boolean hasRole(String expectedRoleValue) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Object[] grr = auth.getAuthorities().toArray();
    String userRoleValue = grr[grr.length - 1].toString();

    Role currentUserRole = Role.getRoleByLabel(userRoleValue);
    Role expectedRole = Role.getRoleByLabel(expectedRoleValue);

    return currentUserRole.ordinal() >= expectedRole.ordinal();
}

From source file:waffle.spring.boot.demo.DemoController.java

@RequestMapping
public String demo(Authentication auth) {
    return String.format("Hello, %s. You have authorities: %s", auth.getPrincipal(),
            auth.getAuthorities().stream().map(a -> a.getAuthority()).collect(Collectors.joining(", ")));
}

From source file:org.sharetask.security.TaskAssigneeOrCreatorPermission.java

private boolean isAuthenticated(final Authentication authentication) {
    return authentication != null
            && authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_USER"));
}

From source file:org.trustedanalytics.user.current.AuthDetailsFinder.java

@Override
public UserRole getRole(Authentication authentication) {
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    if (authorities == null || authorities.isEmpty())
        return UserRole.USER;

    boolean isAdmin = authorities.stream().map(GrantedAuthority::getAuthority)
            .filter(ADMIN_ROLE::equalsIgnoreCase).count() > 0;

    return isAdmin ? UserRole.ADMIN : UserRole.USER;
}

From source file:com.sample.webserviceprocess.security.RoleVoter.java

Collection<? extends GrantedAuthority> extractAuthorities(Authentication authentication) {
    return authentication.getAuthorities();
}