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:org.apache.nifi.minifi.c2.security.authorization.PrincipalStringAuthorityGranter.java

@Override
public Collection<GrantedAuthority> grantAuthorities(Authentication authentication) {
    List<String> authorities = grantedAuthorityMap.get(authentication.getPrincipal().toString());
    if (authorities == null) {
        return null;
    }//  w  ww  .  ja v  a 2s.  c om
    return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
}

From source file:edu.umn.msi.tropix.webgui.server.security.impl.AuthenticationEventHandler.java

public void publishAuthenticationSuccess(final Authentication authentication) {
    LOG.debug("Authentication succeeded for principal " + authentication.getPrincipal());
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:de.steilerdev.myVerein.server.security.rest.RestLogoutSuccessHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    User currentUser = (User) authentication.getPrincipal();
    logger.info("[{}] Successfully logged user out from IP {}", currentUser,
            SecurityHelper.getClientIpAddr(request));
    if (!response.isCommitted()) {
        response.sendError(HttpServletResponse.SC_OK, "Successfully logged out");
    }//from ww w. jav a  2 s. c  om
}

From source file:oobbit.security.JpaAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication a) throws AuthenticationException {
    String username = a.getPrincipal().toString();
    String password = a.getCredentials().toString();

    try {/*  w w w  .  ja va  2s  .  c om*/
        User user = users.attemptLogin(username, password);
        return new UsernamePasswordAuthenticationToken(user.getUsername(), password,
                accessLevelToGrantedAuthority.accessLevelToSimpleGrantedAuthorityList(user.getAccessLevel()));
    } catch (SQLException ex) {
        Logger.getLogger(JpaAuthenticationProvider.class.getName()).log(Level.SEVERE,
                "SQLException was thrown while authenticating a user in JpaAuthenticationProvider.", ex);
    } catch (FailedLoginException ex) {
        Logger.getLogger(JpaAuthenticationProvider.class.getName()).log(Level.SEVERE,
                "FailedLoginException was thrown while tyring to authenticate a user.", ex);
    }

    throw new AuthenticationException("Unable to authenticate user " + username) {
    };
}

From source file:org.openmhealth.shim.AccessParameterClientTokenServices.java

@Override
public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource,
        Authentication authentication) {
    String username = authentication.getPrincipal().toString();
    String shimKey = authentication.getDetails().toString();

    AccessParameters accessParameters = accessParametersRepo.findByUsernameAndShimKey(username, shimKey,
            new Sort(Sort.Direction.DESC, "dateCreated"));

    if (accessParameters == null || accessParameters.getSerializedToken() == null) {
        return null; //No token was found!
    }/*from  www  .  java 2  s.  c  o m*/

    return SerializationUtils.deserialize(accessParameters.getSerializedToken());
}

From source file:edu.eci.test.AAAUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String user = authentication.getPrincipal().toString();
    String pwd = authentication.getCredentials().toString();

    //PUT Auth Bean here

    boolean result = user.equals("myuser") && pwd.equals("mypassword");
    //= aaaProxy.isValidUser(authentication.getPrincipal()
    //.toString(), authentication.getCredentials().toString());

    if (result) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        AAAUserAuthenticationToken auth = new AAAUserAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), grantedAuthorities);

        return auth;
    } else {//from ww w .  j a  v a  2  s .c om
        throw new BadCredentialsException("Bad User Credentials.");
    }

}

From source file:eu.supersede.fe.rest.LocaleRest.java

@RequestMapping("/current")
public Locale getCurrentLocale(Authentication auth) {
    Object user = auth.getPrincipal();
    String l = "any";

    if (user instanceof DatabaseUser) {
        DatabaseUser dbUser = (DatabaseUser) user;

        if (dbUser.getLocale() != null && !dbUser.getLocale().equals("")) {
            l = dbUser.getLocale();/*from  www.  j a  v  a  2s.c o  m*/
        }
    }

    return new Locale(l, "");
}

From source file:eu.enhan.timelord.domain.security.TimelordUserDetailsService.java

public TimelordUser getUserFromSession() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    Object principal = auth.getPrincipal();
    if (principal instanceof TimelordUserDetails) {
        return ((TimelordUserDetails) principal).getUser();
    }/*from w w  w .  j  a  v  a 2 s .co  m*/
    return null;
}

From source file:rzd.vivc.documentexamination.service.AuthenticationInfoSpringSecurity.java

@Override
public String getLogin() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Object principal = authentication.getPrincipal();
    String username;/* ww w  .j  ava 2s.  c  o m*/
    if (principal instanceof UserDetails) {
        username = ((UserDetails) principal).getUsername();
    } else {
        username = principal.toString();
    }
    return username;
}

From source file:cz.muni.fi.pb138.cvmanager.controller.BaseController.java

/**
 * Finds out and returns name of currently logged in user
 *
 * @return name of currently logged in user
 *//*from  w w  w  .j a va 2 s  .c om*/
protected String getPrincipalUsername() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    UserDetails userDetails = (UserDetails) auth.getPrincipal();
    return userDetails.getUsername();
}