Example usage for org.springframework.security.core Authentication getPrincipal

List of usage examples for org.springframework.security.core Authentication getPrincipal

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getPrincipal.

Prototype

Object getPrincipal();

Source Link

Document

The identity of the principal being authenticated.

Usage

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

public static String getCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    String userName = null;//from   w ww. 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:com.wiiyaya.framework.provider.utils.ConsumerUtils.java

@SuppressWarnings("unchecked")
public static <T extends UserDetails> T getCurrUser() {
    Authentication authentication = getAuthentication();
    if (authentication == null) {
        return null;
    } else {/*  ww  w.  j  a va2s .com*/
        return (T) authentication.getPrincipal();
    }
}

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 www . jav a2  s. c  o  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:edu.zipcloud.cloudstreetmarket.core.util.AuthenticationUtil.java

public static UserDetails getPrincipal() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof UserDetails) {
                return (UserDetails) principal;
            }//from  ww  w  .  jav a2  s.c  o  m
        }
    }
    return new User();
}

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;
            }/* w  ww . j av a 2s .co m*/
        }
    }
    return false;
}

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

public static User getUserPrincipal() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof User) {
                return (User) principal;
            }//from w  w w .j  a  va2 s  .c  o m
        }
    }
    return new User();
}

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

/**
 * ????, ?.//  w w  w . j a v a  2  s  .  com
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();
    if (authentication != null && authentication.getPrincipal() != null) {
        return authentication.getName();
    }
    return "";
}

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 w  w w .j a  v  a2  s . c o  m*/
        // Allow exception handling to cover authentication issues
        SLIPrincipal principal = new SLIPrincipal();
        principal.setName("");
        principal.setId("");
        return principal;
    }
}

From source file:com.companyname.extension.PlatAuthentication.java

public static Authentication getPlatAuthentication(Authentication authentication) {

    if (authentication == null) {
        return null;
    }/*from   w  w  w.ja  va  2 s .c  om*/

    PlatAuthentication auth = new PlatAuthentication(authentication.getPrincipal(),
            authentication.getCredentials(), authentication.getAuthorities());

    BeanUtils.copyProperties(authentication, auth, new String[] { "authenticated" });

    return auth;
}