Example usage for org.springframework.security.core.context SecurityContext getAuthentication

List of usage examples for org.springframework.security.core.context SecurityContext getAuthentication

Introduction

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

Prototype

Authentication getAuthentication();

Source Link

Document

Obtains the currently authenticated principal, or an authentication request token.

Usage

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

private static org.springframework.security.core.userdetails.UserDetails getLoggedInUserDetails() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if ((securityContext != null) && (securityContext.getAuthentication() != null)
            && (securityContext.getAuthentication().getPrincipal() != null)
            && (securityContext.getAuthentication()
                    .getPrincipal() instanceof org.springframework.security.core.userdetails.UserDetails))
        return (org.springframework.security.core.userdetails.UserDetails) securityContext.getAuthentication()
                .getPrincipal();/*from   w ww  . j a  va 2 s. co m*/
    return null;
}

From source file:com.miserablemind.butter.helpers.Utilities.java

/**
 * Gets Authenticated User Id from {@link SecurityContext}. Intended use: DAO for "updaterId" audit field population in database.
 *
 * @return {@code 0} if no context, no principal or principal is {@code anonymousUser}, otherwise UserId.
 *///  www.j  a  v a 2 s .c  o m
public static long getAuthUserId() {

    SecurityContext securityContext = Utilities.getSecurityContext();
    if (securityContext == null)
        return 0;
    Object principal = securityContext.getAuthentication().getPrincipal();
    if (principal == null || principal == "anonymousUser")
        return 0;
    AppUser user = (AppUser) principal;
    return user.getId();
}

From source file:org.client.one.service.UserIdentityDetailsService.java

/**
* Returns the domain User object for the currently logged in user, or null
* if no User is logged in./*from  www .  j  a  v  a  2 s.c o m*/
* 
* @return User object for the currently logged in user, or null if no User
*         is logged in.
*/
public static UserIdentity getCurrentUser() {
    SecurityContext sc = SecurityContextHolder.getContext();
    if (sc == null)
        return null;
    if (sc.getAuthentication() == null)
        return null;
    Object principal = sc.getAuthentication().getPrincipal();

    if (principal instanceof UserIdentity)
        return ((UserIdentity) principal);

    // principal object is either null or represents anonymous user -
    // neither of which our domain User object can represent - so return null
    return null;
}

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

/**
 * ?Authentication, ?SecurityContextnull.
 *///  w ww .j  av  a2  s  .c  om
private static Authentication getAuthentication() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        return context.getAuthentication();
    }
    return null;
}

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

public static boolean isAdmin() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(Constants.ROLE_IT_ADMINISTRATOR)) {
                    return true;
                }//from   w  w  w.j a  va  2s . c om
            }
        }
    }
    return false;
}

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

/**
 * find if a user is IT Administrator or Leader
 *
 * @return//from w w  w  .j a va 2  s  . com
 */
public static boolean isNotEducator() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Collection<GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(Constants.ROLE_IT_ADMINISTRATOR)) {
                    return true;
                } else if (authority.getAuthority().equals(Constants.ROLE_LEADER)) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:de.chludwig.websec.saml2sp.springconfig.CurrentUserHandlerMethodArgumentResolver.java

public static ApplicationUser getCurrentUser() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication auth = (securityContext == null) ? null : securityContext.getAuthentication();
    if (auth == null || auth instanceof AnonymousAuthenticationToken) {
        return new AnonymousUser();
    } else {//from www  . jav a2  s . co m
        SAMLCredential samlCredential = getSamlCredential(auth);
        String userId = getUserId(auth, samlCredential);
        String userName = getUserName(auth, samlCredential);
        Set<RoleId> roles = getUserRoles(auth);
        AuthenticationStatus authenticationStatus = getAuthenticationStatus(samlCredential);
        return new LoggedInUser(userId, userName, roles, authenticationStatus);
    }
}

From source file:edu.zipcloud.cloudstreetmarket.core.util.AuthenticationUtil.java

public static boolean isThePrincipal(String userId) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null && !StringUtils.isBlank(userId)) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof UserDetails && userId.equals(((UserDetails) principal).getUsername())) {
                return true;
            }/*from w ww .j  a  va  2  s.  co m*/
        }
    }
    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);
            }/* www  .j  a  v a 2s. c  om*/
            roleSet.add(auth);
        }
    }
    return Collections.unmodifiableSet(roleSet);
}

From source file:org.brekka.pegasus.core.services.impl.AccessorContextImpl.java

private static AccessorContext accessorContext(final boolean useStub) {
    AccessorContext accessorContext = null;
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof PegasusPrincipalAware) {
            PegasusPrincipal pegasusPrincipal = ((PegasusPrincipalAware) principal).getPegasusPrincipal();
            MemberContext memberContext = pegasusPrincipal.getMemberContext();
            if (memberContext == null) {
                throw new PegasusException(PegasusErrorCode.PG623,
                        "No AccessorContext available for authentication: %s", authentication);
            }//from w w  w  .  j av  a 2  s.c  o m
            accessorContext = memberContext.getAccessorContext();
        }
        if (principal instanceof AccessorContextAware) {
            accessorContext = ((AccessorContextAware) principal).getAccessorContext();
        }
    }
    if (accessorContext == null) {
        if (useStub) {
            accessorContext = new AccessorContextImpl();
        } else {
            throw new PegasusException(PegasusErrorCode.PG623,
                    "No AccessorContext available for the current security context '%s'",
                    authentication != null ? authentication.getClass().getName() : null);
        }
    }
    return accessorContext;
}