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 String getUserName() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication.getPrincipal() instanceof UserDetails) {
        return ((UserDetails) authentication.getPrincipal()).getUsername();
    } else {/*  www .  j  a va 2 s. c o m*/
        return authentication.getPrincipal().toString();
    }
}

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

public static String obtainLoggedinUsername() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof UserAccount) {
        return ((UserAccount) principal).getUserName();
    } else {//  w  w w  .  j  av a  2s . com
        throw new IllegalStateException("Wrong security implementation, expecting a UserAccount as principal");
    }
}

From source file:sample.data.jpa.utils.SecurityUtils.java

public static String getLoggedInUserName() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String name = authentication.getName();
    return name;//w  ww.  j  av  a  2s. com
}

From source file:com.greglturnquist.spring.social.ecobee.SignInUtils.java

public static void signin(String userId) {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(userId, null, null));
}

From source file:com.beto.test.securityinterceptor.model.util.ProjectUtil.java

public static User getUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    User user = (User) authentication.getPrincipal();
    return user;//from   w  w w.  j a v  a2 s. c o  m
}

From source file:co.com.soinsoftware.altablero.utils.AuthenticationUtils.java

public static void setAnonymusAuthentication() {
    SecurityContextHolder.getContext().setAuthentication(null);
    SecurityContextHolder.clearContext();
}

From source file:com.faujnet.utils.SignInUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 *///w w w  .  j a v  a 2  s  .  com
public static void signin(String userId) {

    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(userId, null, null));
}

From source file:com.nec.core.context.RootSecurityContext.java

/**
 * Obtains the currently authenticated principal, or an authentication
 * request token.//from w  w w.j av a  2 s.c om
 * 
 * @return the Authentication or null if no authentication information is
 *         available
 */
public static Object getPrincipal() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        return authentication.getPrincipal();
    }
    return null;
}

From source file:com.inkubator.securitycore.util.UserInfoUtil.java

public static String getUserName() {
    UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return user.getUsername();
}

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 a  va 2 s  .  c om
    if (ctx != null) {
        auth = ctx.getAuthentication();
    }
    return ((AuthenticatedUserInfo) auth.getPrincipal()).getLoggedInUserId();
}