Example usage for org.springframework.security.core.userdetails UserDetails getAuthorities

List of usage examples for org.springframework.security.core.userdetails UserDetails getAuthorities

Introduction

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

Prototype

Collection<? extends GrantedAuthority> getAuthorities();

Source Link

Document

Returns the authorities granted to the user.

Usage

From source file:com.sshdemo.common.web.util.ContextUtil.java

private static Collection<String> getGrantedAuthorities(String perfix) {

    List<String> names = new ArrayList<String>();

    UserDetails user = getUserDetails();
    if (user == null) {
        return names;
    }//from  w w w . java 2s.c om

    Collection<? extends GrantedAuthority> authorites = user.getAuthorities();
    for (GrantedAuthority auth : authorites) {
        if (StringUtils.startsWith(auth.getAuthority(), perfix)) {
            names.add(auth.getAuthority());
        }
    }

    return names;
}

From source file:com.ewcms.web.util.EwcmsContextUtil.java

private static Collection<String> getGrantedAuthorities(String perfix) {

    List<String> names = new ArrayList<String>();

    UserDetails user = getUserDetails();
    if (user == null) {
        return names;
    }//from   w  w  w .  j  a v a2  s .  c o  m

    Collection<GrantedAuthority> authorites = user.getAuthorities();
    for (GrantedAuthority auth : authorites) {
        if (StringUtils.startsWith(auth.getAuthority(), perfix)) {
            names.add(auth.getAuthority());
        }
    }

    return names;
}

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  ww  w. j  a  v a2s  .c o m*/
        }
    }
    log.debug("Not Authorized. User has not been granted any of: {}", roles);
    return false;
}

From source file:com.brienwheeler.web.spring.security.SecurityUtils.java

private static Collection<? extends GrantedAuthority> getLoggedInUserGrantedAuthorities() {
    org.springframework.security.core.userdetails.UserDetails userDetails = getLoggedInUserDetails();
    if (userDetails == null)
        return new ArrayList<GrantedAuthority>();
    return userDetails.getAuthorities();
}

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

/**
 * UserDetails?Security Context./*ww  w  .java 2  s .c o m*/
 * 
 * @param userDetails ??.
 * @param request ?IP??.
 */
public static void saveUserDetailsToContext(UserDetails userDetails, HttpServletRequest request) {
    PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(userDetails,
            userDetails.getPassword(), userDetails.getAuthorities());

    authentication.setDetails(new WebAuthenticationDetails(request));

    SecurityContextHolder.getContext().setAuthentication(authentication);
}

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

/**
 * UserDetails?Security Context./*ww w. j a v  a  2 s . c  o m*/
 * 
 * @param userDetails ??.
 * @param request ?IP??,?Null.
 */
public static void saveUserDetailsToContext(UserDetails userDetails, HttpServletRequest request) {
    PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(userDetails,
            userDetails.getPassword(), userDetails.getAuthorities());

    if (request != null) {
        authentication.setDetails(new WebAuthenticationDetails(request));
    }

    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:com.minlia.cloud.framework.common.security.SpringSecurityUtil.java

/**
 * Check if current user has specified role.
 *
 * @param privilege/*from w w w  . j a  va2  s. co  m*/
 *            the role to check if user has.
 * @return true if user has specified role, otherwise false.
 */
public static boolean hasPrivilege(final String privilege) {
    final UserDetails userDetails = SpringSecurityUtil.getCurrentUserDetails();
    if (userDetails != null) {
        for (final GrantedAuthority each : userDetails.getAuthorities()) {
            if (each.getAuthority().equals(privilege)) {
                return true;
            }
        }
    }

    return false;
}

From source file:com.minlia.cloud.framework.common.security.SpringSecurityUtil.java

/**
 * Check if current user has any role of specified.
 *
 * @param privileges//  w  ww  .j  av a2s  .c  o m
 *            the array of roles.
 * @return true if has any role, otherwise false.
 */
public static boolean hasAnyPrivilege(final String... privileges) {
    final UserDetails userDetails = SpringSecurityUtil.getCurrentUserDetails();
    if (userDetails != null) {
        final Set<String> rolesSet = ImmutableSet.copyOf(privileges);
        for (final GrantedAuthority each : userDetails.getAuthorities()) {
            if (rolesSet.contains(each.getAuthority())) {
                return true;
            }
        }
    }

    return false;
}

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

/**
 * Rebuild an Authentication for the given username and register it in the security context.
 * Typically used after updating a user's authorities or other auth-cached info.
 * <p/>/*w w  w .  j  a  v a 2 s . c o m*/
 * Also removes the user from the user cache to force a refresh at next login.
 *
 * @param username the user's login name
 * @param password optional
 */
public static void reauthenticate(final String username, final String password) {
    UserDetailsService userDetailsService = getBean("userDetailsService");
    UserCache userCache = getBean("userCache");

    UserDetails userDetails = userDetailsService.loadUserByUsername(username);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails,
            password == null ? userDetails.getPassword() : password, userDetails.getAuthorities()));
    userCache.removeUserFromCache(username);
}

From source file:org.itracker.web.util.LoginUtilities.java

/**
 * Returns true if the user has the required permission for the given project.
 *
 * @param project project to which permission is checked for
 * @param permissionNeeded the permission to check for
 *///from   w  w  w  .  j  a  va 2s .  c  o  m
public static boolean hasPermission(Project project, PermissionType permissionNeeded) {
    UserDetails user = getPrincipal();
    if (null == user) {
        return false;
    }
    if (permissionNeeded != PermissionType.USER_ADMIN && hasPermission(PermissionType.USER_ADMIN)) {
        return true;
    } else if (null != project && permissionNeeded != PermissionType.PRODUCT_ADMIN
            && hasPermission(project, PermissionType.PRODUCT_ADMIN)) {
        return true;
    }
    Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
    String permissionName = permissionNeeded.name(project);
    for (GrantedAuthority authority : authorities) {
        if (authority.getAuthority().equals(permissionName)) {
            return true;
        }
    }
    return false;
}