Example usage for org.springframework.security.core Authentication getName

List of usage examples for org.springframework.security.core Authentication getName

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getName.

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:com.minlia.cloud.framework.common.security.SpringSecurityUtil.java

public static String getNameOfCurrentPrincipal() {
    final Authentication authentication = getCurrentAuthentication();
    if (authentication == null) {
        return null;
    }//from   w w w.j  a v a2s.co  m

    return authentication.getName();
}

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.
 * /*from  w w  w  . ja  v  a2 s.  c  om*/
 * @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:com.task.springsec.SecurityUtil.java

/**
 * test if current user principal contains all given authorities
 *   /*from  w w  w .j a v a 2s . co m*/
 * @param authorities the roles will be checked 
 */
public static void assertAll(String... authorities) {

    if (null == authorities || authorities.length == 0) {
        return;
    }

    final ArrayList<GrantedAuthority> required = new ArrayList<GrantedAuthority>();
    for (String auth : authorities) {
        required.add(new GrantedAuthorityImpl(auth));
    }

    final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
    if (!granted.containsAll(required)) {
        Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
        throw new AccessDeniedException("The current principal doesn't has enough authority. Authentication: "
                + (currentUser == null ? "" : currentUser.getName()));
    }
}

From source file:id.co.sigma.zk.spring.security.SecurityUtil.java

/**
 * test if current user principal contains all given authorities
 *   /*from ww w .j  av  a 2 s  .  c o m*/
 * @param authorities the roles will be checked 
 */
public static void assertAll(String... authorities) {

    if (null == authorities || authorities.length == 0) {
        return;
    }

    final ArrayList<GrantedAuthority> required = new ArrayList<GrantedAuthority>();
    for (String auth : authorities) {
        required.add(new SimpleGrantedAuthority(auth));
    }

    final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
    if (!granted.containsAll(required)) {
        Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
        throw new AccessDeniedException("The current principal doesn't has enough authority. Authentication: "
                + (currentUser == null ? "" : currentUser.getName()));
    }
}

From source file:nc.noumea.mairie.annuairev2.saisie.core.security.SecurityUtil.java

/**
 * test if current user principal contains all given authorities
 *
 * @param authorities/*from   ww  w .  j av  a2s . c  o m*/
 *            the roles will be checked
 */
public static void assertAll(String... authorities) {

    if (null == authorities || authorities.length == 0) {
        return;
    }

    final ArrayList<GrantedAuthority> required = new ArrayList<>();
    for (String auth : authorities) {
        required.add(new SimpleGrantedAuthority(auth));
    }

    final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
    if (!granted.containsAll(required)) {
        Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
        throw new AccessDeniedException("The current principal doesn't has enough authority. Authentication: "
                + (currentUser == null ? "" : currentUser.getName()));
    }
}

From source file:nc.noumea.mairie.annuairev2.saisie.core.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 * @return /*from  w w  w. ja va 2s  .co m*/
 */
public static String getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof LdapUserDetails)
                return ((LdapUserDetails) p).getUsername();
            else {
                if ("test".equalsIgnoreCase(ApplicationContextUtils.getActiveProfile())) {
                    // env. test
                    return auth.getName();
                }
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    return null;
}

From source file:org.socialhistoryservices.pid.util.NamingAuthority.java

public static List<String> getNaRole(Authentication userAuthentication) {

    final Collection<? extends GrantedAuthority> authorities = userAuthentication.getAuthorities();
    final List<String> nas = new ArrayList(authorities.size());
    for (GrantedAuthority authority : authorities) {
        String role = authority.getAuthority().replace("\n", ""); // ToDo: find out if there still is a \n in the role.
        if (role.startsWith(role_prefix)) {
            nas.add(role.substring(role_prefix.length()));
        } else if (role.startsWith(role_prefix_deprecated)) {
            nas.add(role.substring(role_prefix_deprecated.length()));
        }//from w  w w .j  av a2  s.  c  o  m
    }
    if (nas.size() == 0)
        throw new SecurityException("User " + userAuthentication.getName()
                + " has not got the required roles to use this service.");
    return nas;
}

From source file:com.example.MyService.java

public String hello(Authentication authentication) {
    return "Hello " + authentication.getName() + "!\n";
}

From source file:shiver.me.timbers.security.spring.AuthenticatedAuthenticationConverter.java

@Override
public String convert(Authentication authentication) {
    return authentication.getName();
}

From source file:oauth2.endpoints.UserDetailsEndpoint.java

@RequestMapping(Urls.USER)
public User userDetails(Authentication user) {
    return new User(user.getName(), user.getName());
}