Example usage for org.springframework.security.core.context SecurityContextHolder getContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContextHolder getContext.

Prototype

public static SecurityContext getContext() 

Source Link

Document

Obtain the current SecurityContext.

Usage

From source file:com.sentinel.util.SecurityUtil.java

public static ArrayList<String> getUserRoles() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
    ArrayList<String> currentUserRoles = new ArrayList<String>();
    for (GrantedAuthority authority : authorities) {
        currentUserRoles.add(authority.getAuthority());
    }/*from   w w  w .j a v  a 2s. co  m*/
    if (LOG.isDebugEnabled()) {
        LOG.debug("currentUserRoles:" + currentUserRoles);
    }
    return currentUserRoles;
}

From source file:org.devgateway.toolkit.forms.security.SecurityUtil.java

/**
 * returns the principal object. In our case the principal should be
 * {@link Person}/*from w w w . j  a v a2s . com*/
 * 
 * @return the principal or null
 * @see Principal
 */
public static Person getCurrentAuthenticatedPerson() {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        return null;
    }
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    final Object principal = authentication.getPrincipal();
    if (principal instanceof Person) {
        return (Person) principal;
    }
    return null;
}

From source file:org.socialsignin.springsocial.security.signin.AuthenticatedUserIdHolder.java

/**
 * @return The userId of the currently authenticated user
 * or null if the local user is anonymous
 *///from ww  w.  ja  v a2s .c o m
public static String getAuthenticatedUserId() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    return auth == null || auth.getName().equals("anonymousUser") ? null : auth.getName();
}

From source file:org.sharetask.utility.SecurityUtil.java

/**
 * Get current logged in user.//w  w  w. j av  a 2s.c  o m
 * @return
 */
public static String getCurrentSignedInUsername() {
    return SecurityContextHolder.getContext().getAuthentication().getName();
}

From source file:com.excilys.ebi.bank.web.security.SecurityUtils.java

public static User getCurrentUser() {
    if (isAuthenticated()) {
        return CustomUserDetails.class
                .cast(SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
    }/*from w  w w . ja  v  a  2 s.  com*/
    return null;
}

From source file:org.cloudbyexample.dc.service.util.SecurityContextUtil.java

public static void createSystemUser() {
    SecurityContext context = SecurityContextHolder.getContext();

    // Set the granted authorities so that the user is not authenticated again
    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_SYSTEM"));

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("system", "",
            grantedAuthorities);//  w  ww.j ava  2s. c  om

    context.setAuthentication(token);
}

From source file:edu.wisc.web.security.portlet.primaryattr.PrimaryAttributeUtils.java

/**
 * @return The current user's primary attribute
 *//*ww  w .ja va  2s  . co m*/
public static String getPrimaryId() {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();

    final PrimaryAttributePortletAuthenticationDetails authenticationDetails = (PrimaryAttributePortletAuthenticationDetails) authentication
            .getDetails();
    return authenticationDetails.getPrimaryAttribute();
}

From source file:com.ai.bss.webui.util.SecurityUtil.java

public static String obtainLoggedinUserIdentifier() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof UserAccount) {
        return ((UserAccount) principal).getUserId();
    } else {//from  w  w w  . ja va  2s .  c om
        throw new IllegalStateException("Wrong security implementation, expecting a UserAccount as principal");
    }
}

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

public static UserDetails getPrincipal() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if ((authentication != null) && (authentication.getPrincipal() instanceof UserDetails)) {
        return (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    } else {/*from www  . j ava  2s.  c o  m*/
        // Allow exception handling to cover authentication issues
        SLIPrincipal principal = new SLIPrincipal();
        principal.setName("");
        principal.setId("");
        return principal;
    }
}

From source file:pl.touk.wonderfulsecurity.core.ServerSecurity.java

/**
 * Return currently logged in user//from ww w . j av  a2  s .c  o m
 */
public static WsecUser getLoggedInUser() {
    Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    if (obj instanceof UserDetails) {
        WsecUserDetails wsecUserDetails = (WsecUserDetails) obj;
        return wsecUserDetails.getWsecUser();
    }
    throw new IllegalStateException("No one logged in!!!");
}