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:x1.markdown.security.SecurityUtils.java

public static String getCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    String userName = null;/*from  w  w  w . ja  v a  2  s . c o  m*/
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof UserDetails) {
            UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
            userName = springSecurityUser.getUsername();
        } else if (authentication.getPrincipal() instanceof String) {
            userName = (String) authentication.getPrincipal();
        }
    }
    return userName;
}

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

/**
 * @return The current user's primary attribute
 *//*  www . j  a  v a 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:edu.wisc.portlet.hrs.web.EmplIdUtils.java

/**
 * @return The current user's EmplID/*from ww  w.  ja va 2s  .  com*/
 */
public static String getEmplId() {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();

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

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

public static boolean isAuthenticated() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    return ctx != null //
            && ctx.getAuthentication() != null //
            && ctx.getAuthentication().isAuthenticated();
}

From source file:com.pamarin.income.security.SecurityUtils.java

public static User getUser() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    User user = null;//from   ww w .j  av  a2  s.  co m

    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            user = (User) principal;
        } else {
            user = new User(ANONYMOUS, null);
        }
    }

    return user;
}

From source file:com.marklogic.samplestack.domain.ClientRole.java

/**
 * Provides the username for implied by the security context for the spring application.
 * @return The username value that corresponds to the current logged-in user.
 *//*from   w  w w.  ja  v a2  s. c om*/
public static String securityContextUserName() {
    SecurityContext secContext = SecurityContextHolder.getContext();
    String userName = secContext.getAuthentication().getName();
    return userName;
}

From source file:com.rockagen.gnext.service.spring.security.aspect.SS3Tools.java

/**
 * Obtain current user principal/*  w w w. j a  v a2  s .  com*/
 * @return BasicSecurityUser
 */
public static BasicSecurityUser getUserInfo() {
    BasicSecurityUser userDetails = null;
    try {
        SecurityContext sc = SecurityContextHolder.getContext();
        Authentication au = sc.getAuthentication();
        userDetails = (BasicSecurityUser) au.getPrincipal();
    } catch (NullPointerException e) {
        userDetails = new BasicSecurityUser("guest", "guest", true, true, true, true,
                AuthorityUtils.NO_AUTHORITIES, "root", "", "127.0.0.1");

    }
    return userDetails;

}

From source file:com.utest.domain.service.util.UserUtil.java

public static Integer getCurrentUserId() {
    final SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = null;/*from   w  w  w . j  av  a2 s  . co m*/
    if (ctx != null) {
        auth = ctx.getAuthentication();
    }
    return ((AuthenticatedUserInfo) auth.getPrincipal()).getLoggedInUserId();
}

From source file:de.pksoftware.springstrap.basic.service.BasicSecurityService.java

/**
 * Get the UserDetails of the current logged in Account.
 * @return//w ww .  j  a va 2  s .  c om
 */
public static IAccount getCurrentUserDetails() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication auth = securityContext.getAuthentication();
    if (null != auth && !(auth instanceof AnonymousAuthenticationToken)) {
        return (IAccount) auth.getPrincipal();
    }
    return null;
}

From source file:org.zalando.stups.oauth2.spring.client.AccessTokenUtils.java

public static Optional<String> getAccessTokenFromSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.getContext();

    Authentication authentication = securityContext.getAuthentication();
    if (authentication instanceof OAuth2Authentication) {
        Object userDetails = ((OAuth2Authentication) authentication).getUserAuthentication().getDetails();
        if (userDetails != null) {
            try {
                final Map details = (Map) userDetails;
                return Optional.fromNullable((String) details.get(ACCESS_TOKEN));
            } catch (ClassCastException e) {

                return Optional.absent();
            }/*from  w  w w.  ja v a 2  s .  c o  m*/
        } else {

            return Optional.absent();
        }
    }

    return Optional.absent();
}