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:org.terasoluna.tourreservation.app.common.security.UserDetailsUtils.java

public static ReservationUserDetails getUserDetails(Authentication auth) {
    if (auth != null && auth.getPrincipal() instanceof ReservationUserDetails) {
        return ((ReservationUserDetails) auth.getPrincipal());
    }//from   w  ww  .j a va  2 s .  c o  m

    return null;
}

From source file:shiver.me.timbers.spring.security.integration.CustomPrincipleAuthenticationConverter.java

private static String extractUsername(Authentication authentication) {
    final Object principal = authentication.getPrincipal();
    if (principal instanceof UserDetails) {
        return ((UserDetails) principal).getUsername();
    }//www. j a v a  2  s .  co m

    return principal.toString();
}

From source file:sample.contact.Utils.java

public static String getUsername() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (auth.getPrincipal() instanceof UserDetails) {
        return ((UserDetails) auth.getPrincipal()).getUsername();
    } else {/* ww  w  .j  a  v a  2 s.c  om*/
        return auth.getPrincipal().toString();
    }
}

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;/*  w  w  w.  j  av a2s .  c o m*/
}

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 {//from   w ww  .j av  a  2 s  . c o  m
        return authentication.getPrincipal().toString();
    }
}

From source file:com.autoupdater.server.utils.authentication.CurrentUserUtil.java

/**
 * Returns current user's name or null if none authenticated;
 * /*from  w  w  w .j  a va2s.com*/
 * @return username if available, null otherwise
 */
public static String getUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication != null ? (String) authentication.getPrincipal() : null;
}

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

/**
 * Obtain current user principal//from  w ww  . j a v a2  s  . c  om
 * @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;
    if (ctx != null) {
        auth = ctx.getAuthentication();//from   w  ww .  j a v  a 2  s  .  c  o  m
    }
    return ((AuthenticatedUserInfo) auth.getPrincipal()).getLoggedInUserId();
}

From source file:com.erudika.para.security.SecurityUtils.java

/**
 * Extracts a User object from the security context
 * @return an authenticated user or null if a user is not authenticated
 *//*from w w  w . ja  va2s  . c  o  m*/
public static User getAuthenticatedUser() {
    User u = null;
    if (SecurityContextHolder.getContext().getAuthentication() != null) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth.isAuthenticated() && auth.getPrincipal() instanceof User) {
            u = (User) auth.getPrincipal();
        }
    }
    return u;
}

From source file:org.messic.server.facade.security.SecurityUtil.java

/**
 * Return the current user logged. Throws an exception if nobody logged depending on the parameter completeUser, the
 * function will return an {@link User} with only the username, or an {@link User} with all the information.
 * //  w  w w . j ava  2  s.c o  m
 * @param completeUser boolean flag to know if the returned user must be complete with all the user information or
 *            just the login information
 * @param daoUser {@link DAOUser} this param is only necessary when param completeUser is true. If not, it can be
 *            null
 * @return {@link User} user logged
 * @throws NotAuthorizedMessicRESTException
 */
public static User getCurrentUser(boolean completeUser, DAOUser daoUser)
        throws NotAuthorizedMessicRESTException {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.getPrincipal().equals("anonymousUser")) {
        return null;
    }
    if (auth != null) {
        if (!completeUser) {
            User user = new User();
            user.setLogin(auth.getName());
            return user;
        } else {
            MDOUser mdoUser = daoUser.getUserByLogin(auth.getName());
            User user = new User(mdoUser);
            return user;
        }
    } else {
        return null;
    }
}