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:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasWriteAuthorityOnQueue(QueueEntity queueEntity) {
    EntityManagerFactory configEntityManagerFactory = Persistence.createEntityManagerFactory("fintpCFG");
    EntityManager entityManagerConfig = configEntityManagerFactory.createEntityManager();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    Query query = entityManagerConfig.createQuery(
            "SELECT ur.roleid FROM UserRoleEntity ur, QueuesRoleMapEntity qr " + "WHERE ur.roleid = qr.roleid "
                    + "AND ur.userid=:userid " + "AND qr.queueid=:queueid " + "AND qr.actiontype = 'RW'");
    query.setParameter("userid", loggedUser.getUserid());
    query.setParameter("queueid", queueEntity.getGuid());
    List roles = query.getResultList();
    if (roles.isEmpty()) {
        return false;
    }//from  w  w w  .  ja  v  a 2  s  . c  om
    return true;
}

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

/**
 * ??, SpringSecurityUser?, ?null./* w  ww .j  av  a 2 s.com*/
 */
@SuppressWarnings("unchecked")
public static <T extends User> T getCurrentUser() {
    Authentication authentication = getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            return (T) principal;
        }
    }
    return null;
}

From source file:org.mashupmedia.util.AdminHelper.java

public static User getLoggedInUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }//from  w  w  w.j av  a2 s. com

    Object principal = authentication.getPrincipal();
    if (principal == null) {
        return null;
    }

    if (principal instanceof User) {
        User user = (User) principal;
        return user;
    }

    return null;
}

From source file:org.devgateway.toolkit.forms.security.SecurityUtil.java

/**
 * returns the principal object. In our case the principal should be
 * {@link Person}//w  w  w. j  a  v a2  s . c o  m
 * 
 * @return the principal or null
 * @see Principal
 */
public static Person getCurrentAuthenticatedPerson() {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        return null;
    }
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    final Object principal = authentication.getPrincipal();
    if (principal instanceof Person) {
        return (Person) principal;
    }
    return null;
}

From source file:com.gcrm.util.security.UserUtil.java

/**
 * Gets current login user name//from  www .j  a v a  2s  .  c o  m
 * 
 * @return curretn login user name
 */
public static String getUserName() {
    try {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return userDetails.getUsername();
    } catch (Exception e) {
        return null;
    }
}

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

public static GizmoPrincipal getPrincipalUser(Authentication authentication) {
    if (authentication == null) {
        LOGGER.debug("Authentication not available in security current context holder.");
        return null;
    }//from  ww w . j ava 2s  .  co  m

    Object principal = authentication.getPrincipal();
    if (!(principal instanceof GizmoPrincipal)) {
        LOGGER.debug("Principal user in security context holder is {} but not type of {}",
                new Object[] { principal, GizmoPrincipal.class.getName() });
        return null;
    }

    return (GizmoPrincipal) principal;
}

From source file:com.mothsoft.alexis.security.CurrentUserUtil.java

public static UserAuthenticationDetails getCurrentUser() {
    final SecurityContext ctx = SecurityContextHolder.getContext();
    final Authentication authentication = ctx.getAuthentication();

    try {/*  ww  w.  j  ava2s  .  co  m*/
        return authentication != null && authentication.isAuthenticated()
                ? (UserAuthenticationDetails) authentication.getPrincipal()
                : null;
    } catch (ClassCastException e) {
        throw new AuthenticationServiceException(e.getLocalizedMessage(), e);
    }
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditInfoExtractorTest.java

private static HttpServletRequest getHttpServletRequest() {
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteHost()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteUser())
            .thenReturn("User[id=2, name=admin, group=[UserGroup[id=1, groupName=everyone]], role=ADMIN]");
    Mockito.when(httpServletRequest.getServerName()).thenReturn("localhost");
    UserGroup userGroup = Mockito.mock(UserGroup.class);
    Mockito.when(userGroup.getGroupName()).thenReturn("everyone");
    User user = Mockito.mock(User.class);
    Mockito.when(user.getName()).thenReturn("admin");
    Mockito.when(user.getRole()).thenReturn(Role.ADMIN);
    Mockito.when(user.getGroups()).thenReturn(Collections.singleton(userGroup));
    Authentication authentication = Mockito.mock(Authentication.class);
    Mockito.when(authentication.getPrincipal()).thenReturn(user);
    Mockito.when(httpServletRequest.getUserPrincipal()).thenReturn(authentication);
    return httpServletRequest;
}

From source file:ru.org.linux.auth.AuthUtil.java

public static void updateLastLogin(Authentication authentication, UserDao userDao) {
    if (authentication != null && (authentication.isAuthenticated())) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetailsImpl) {
            UserDetailsImpl userDetails = (UserDetailsImpl) principal;
            User user = userDetails.getUser();
            userDao.updateLastlogin(user, true);
        }//from   w w w  .  j a v  a2 s .  com
    }
}

From source file:org.red5.demo.auth.Application.java

public static boolean isAuthorized(String... roles) {
    // get the auth from the security context
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.isAuthenticated()) {
        UserDetails deets = (UserDetails) auth.getPrincipal();
        log.debug("enabled: {}", deets.isEnabled());
        Collection<GrantedAuthority> granted = deets.getAuthorities();
        for (GrantedAuthority authority : granted) {
            if (Arrays.asList(roles).contains(authority.getAuthority())) {
                log.debug("Authorized");
                return true;
            }// ww  w .j  a va2  s . com
        }
    }
    log.debug("Not Authorized. User has not been granted any of: {}", roles);
    return false;
}