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:dtu.ds.warnme.utils.SecurityUtils.java

public static UserEntity getCurrentUser() {
    if (SecurityContextHolder.getContext() == null
            || SecurityContextHolder.getContext().getAuthentication() == null
            || SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null) {
        return null;
    }/*from   w ww  .  j a  va 2  s  .com*/
    if (!(SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof CustomUser)) {
        return null;
    }
    CustomUser authenticatedUser = (CustomUser) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();
    return authenticatedUser.getUserEntity() == null ? null : authenticatedUser.getUserEntity();
}

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 {//from   w  ww . ja  va2 s . com
        return auth.getPrincipal().toString();
    }
}

From source file:aka.pirana.springsecurity.web.controllers.UserController.java

public static User getCurrentUser() {
    System.out.println("aka.pirana.springsecurity.web.controllers.UserController.getCurrentUser()");
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof UserDetails) {
        String email = ((UserDetails) principal).getUsername();
        User loginUser = userService.findUserByEmail(email);
        return new SecurityUser(loginUser);
    }//from  w ww . ja  va2s  .  c om

    return null;
}

From source file:org.easit.core.controllers.signin.SignInUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 *    //Depercated//from w w  w  . j  av a  2s  . co  m
 */
public static Authentication signin(String userId) {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(userId, null, null));
    return SecurityContextHolder.getContext().getAuthentication();
}

From source file:sk.lazyman.gizmo.security.SecurityUtils.java

public static GizmoPrincipal getPrincipalUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    return getPrincipalUser(authentication);
}

From source file:rights.UserRightsUtil.java

public static boolean isRight(String right) {
    Collection<? extends GrantedAuthority> grantes = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();//  w ww.  jav  a  2 s .  c o  m
    for (GrantedAuthority grant : grantes) {
        if (grant.getAuthority().equals(right)) {
            return true;
        }
    }
    return false;
}

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

/**
 * Returns current user's name or null if none authenticated;
 * // ww w  .  jav a  2s .  c  o  m
 * @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:org.meruvian.yama.web.SessionCredentials.java

public static User getCurrentUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()) {
        return null;
    }//  w w  w  .  ja  v  a  2  s .  c o  m

    if (authentication.getPrincipal() instanceof DefaultUserDetails) {
        DefaultUserDetails user = (DefaultUserDetails) authentication.getPrincipal();
        return user.getUser();
    }

    if (authentication.getPrincipal() instanceof String) {
        String principal = (String) authentication.getPrincipal();
        if ("anonymousUser".equals(principal)) {
            return null;
        }

        User user = new User();
        user.setUsername((String) authentication.getPrincipal());
        return user;
    }

    return null;
}

From source file:com.notemyweb.spring.security.SignInUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 * @return /*from w w w.ja v a  2  s .  com*/
 */
public static Authentication signin(String userId) {
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userId, null,
            null);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return authentication;
}

From source file:com.sitewhere.security.LoginManager.java

/**
 * Get the currently logged in user from Spring Security.
 * //  w  w w .  ja  v  a2 s . c o  m
 * @return
 * @throws SiteWhereException
 */
public static IUser getCurrentlyLoggedInUser() throws SiteWhereException {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        throw new SiteWhereSystemException(ErrorCode.NotLoggedIn, ErrorLevel.ERROR,
                HttpServletResponse.SC_FORBIDDEN);
    }
    if (!(auth instanceof SitewhereAuthentication)) {
        throw new SiteWhereException(
                "Authentication was not of expected type: " + SitewhereAuthentication.class.getName());
    }
    return (IUser) ((SitewhereAuthentication) auth).getPrincipal();
}