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:id.co.sigma.zk.spring.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 *///from  www .  jav a 2  s. c o  m
public static UserData getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof UserData)
                return (UserData) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:BusinessLayer.service.ConsultBalanceService.java

public Compte getAccount(String accountnumber, String password) {
    CustomSecurityUser currentUser = (CustomSecurityUser) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();//from   w ww . j  a v  a  2  s  .  c o m
    System.out.println(currentUser.getId());

    Utilisateur utilisateur = userDAO.getUser(currentUser.getId());

    if (!utilisateur.getEnabled()) {
        throw new DisabledUserProfileException();
    }

    if (!(utilisateur.getPass().equals(password))) {
        throw new WrongPasswordException();
    }

    Compte account = accountDAO
            .getAccount(new CompteId((int) Integer.parseInt(accountnumber), (int) currentUser.getId()));

    if (account == null) {
        throw new AccountNONExistantException();
    }

    return account;

}

From source file:com.example.security.UserAuthentication.java

@Override
public Object getPrincipal() {
    return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

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  ww . j a  v  a 2  s.  co 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;
    }
}

From source file:org.businessmanager.service.security.SpringSecurityServiceImpl.java

@Override
public User getLoggedInUser() {
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return userService.getUserByName(username);
    }//from   ww w  .j  a v  a2  s . com
    return null;
}

From source file:ispok.helper.SessionListener.java

@Override
public void sessionDestroyed(HttpSessionEvent hse) {

    logger.entry();//from   w  ww  .  j a  v  a  2s  . co  m
    logger.debug("Session destroyed: {}", hse.getSession().toString());

    //        Enumeration<String> attributeNames = hse.getSession().getAttributeNames();
    //        logger.debug("Attributes: ");
    //        while (attributeNames.hasMoreElements()) {
    //            logger.debug(attributeNames.nextElement());
    //        }
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null) {
        logger.debug("Username: {}", authentication.getName());
    }

    logger.exit();
}

From source file:org.meruvian.yama.web.SessionCredentials.java

public static List<String> getAuthorities() {
    List<String> roles = new ArrayList<String>();
    Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();/*from  w  w  w  . j  a  v a2  s .c o m*/
    for (GrantedAuthority authority : authorities) {
        roles.add(authority.getAuthority());
    }

    return roles;
}

From source file:org.ngrinder.user.service.UserContext.java

/**
 * Get current user object from context.
 *
 * @return current user;/*from   w  w  w.java 2s  .  c o  m*/
 */
public User getCurrentUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        throw new AuthenticationCredentialsNotFoundException("No authentication");
    }
    Object obj = auth.getPrincipal();
    if (!(obj instanceof SecuredUser)) {
        throw new AuthenticationCredentialsNotFoundException("Invalid authentication with " + obj);
    }
    SecuredUser securedUser = (SecuredUser) obj;
    return securedUser.getUser();
}

From source file:tds.tdsadmin.web.backingbean.UserBean.java

public String getFullName() {
    try {//from   w  w  w.  java 2s .c o m
        SbacUser user = (SbacUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getFullName();
    } catch (Exception e) {
        _logger.error(e.toString(), e);
    }
    return "Unable to retrieve Name";
}

From source file:org.callistasoftware.netcare.web.controller.ControllerSupport.java

protected UserBaseView getLoggedInUser() {
    return (UserBaseView) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}