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.sjtu.onlinelibrary.util.SpringSecurityUtils.java

/**
 * ????, ?.//from   w ww .ja  v a2  s . c om
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }
    return authentication.getName();
}

From source file:com.wisemapping.security.Utils.java

@NotNull
public static User getUser(boolean forceCheck) {
    User result = null;// w  ww . j a va  2  s  .  c o m
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.getDetails() != null) {
        final Object principal = auth.getPrincipal();
        if (principal != null && principal instanceof UserDetails) {
            result = ((UserDetails) principal).getUser();
        }
    }

    if (result == null && forceCheck) {
        throw new IllegalStateException("User could not be retrieved");
    }
    return result;
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ????, ?.//from  w ww.  j  ava2  s.c  om
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }

    return authentication.getName();
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ????, ?./*from  ww  w  . j  a  va 2 s  . c  om*/
 */
public static String getCurrentAccountName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }

    return authentication.getName();
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasReportsRole() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    List<RoleEntity> roles = (List<RoleEntity>) loggedUser.getAuthorities();
    for (int roleIndex = 0; roleIndex < roles.size(); roleIndex++) {
        if (roles.get(roleIndex).getAuthority().equals("Reports")) {
            return true;
        }/*  ww  w . j  a va2 s. c  o m*/
    }
    return false;
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasAdministratorRole() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    List<RoleEntity> roles = (List<RoleEntity>) loggedUser.getAuthorities();
    for (int roleIndex = 0; roleIndex < roles.size(); roleIndex++) {
        if (roles.get(roleIndex).getAuthority().equals("Administrator")) {
            return true;
        }//from   w  w  w .ja va2  s  . com

    }
    return false;
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasUserOrAdministratorRole() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    List<RoleEntity> roles = (List<RoleEntity>) loggedUser.getAuthorities();
    for (int roleIndex = 0; roleIndex < roles.size(); roleIndex++) {
        if (roles.get(roleIndex).getAuthority().equals("Administrator")
                || roles.get(roleIndex).getAuthority().equals("Basic user")) {
            return true;
        }// w  w w.  ja va 2 s  .c  om

    }
    return false;
}

From source file:com.springsource.greenhouse.account.AccountUtils.java

/**
 * Get the currently authenticated Account principal.
 *///from  ww  w  .  ja v  a2s  . co  m
public static Account getCurrentAccount() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    Object principal = authentication.getPrincipal();
    return principal instanceof Account ? (Account) principal : null;
}

From source file:de.pksoftware.springstrap.basic.service.BasicSecurityService.java

/**
 * Get the UserDetails of the current logged in Account.
 * @return//from w  w  w.  j  a  va2 s. c  o  m
 */
public static IAccount getCurrentUserDetails() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication auth = securityContext.getAuthentication();
    if (null != auth && !(auth instanceof AnonymousAuthenticationToken)) {
        return (IAccount) auth.getPrincipal();
    }
    return null;
}

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasReadAuthorityOnQueue(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");
    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 2s .  com*/
    return true;
}