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.deegree.securityproxy.authentication.basic.AddHeaderBasicAuthenticationFilter.java

private void addHeader(HttpServletRequest request, Authentication authResult) {
    OwsUserDetails principal = (OwsUserDetails) authResult.getPrincipal();
    String accessToken = principal.getAccessToken();
    ((AddHeaderHttpServletRequestWrapper) request).addHeader(headerName, accessToken);
}

From source file:org.shredzone.cilla.ws.client.RemoteLoginServiceImpl.java

@Override
public RemoteUserDetails getAuthenticatedUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.getPrincipal() != null && auth.getPrincipal() instanceof RemoteUserDetails) {
        return (RemoteUserDetails) auth.getPrincipal();
    }/*from www . jav a2  s . c  om*/

    return null;
}

From source file:com.boundlessgeo.geoserver.api.controllers.LoginController.java

/**
 * API endpoint for determining if a user is logged in
 * //from w ww  . j av a  2s  . com
 * @param req HTTP request
 * @param res HTTP response
 * @return JSON object containing the session id, the session timeout interval, 
 * and the GeoServer user, if applicable.
 */
@RequestMapping()
public @ResponseBody JSONObj handle(HttpServletRequest req, HttpServletResponse res) {
    JSONObj obj = new JSONObj();

    HttpSession session = req.getSession(false);
    if (session != null) {
        obj.put("session", session.getId());
        obj.put("timeout", session.getMaxInactiveInterval());
    }

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Object principal = auth.getPrincipal();
    if (principal instanceof GeoServerUser) {
        GeoServerUser user = (GeoServerUser) principal;
        obj.put("user", user.getUsername());
        //PKI Authentication
    } else if (auth instanceof PreAuthenticatedAuthenticationToken && principal instanceof String) {
        obj.put("user", principal);
    }

    return obj;
}

From source file:org.shredzone.cilla.ws.cxf.CillaRemoteAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!(authentication.getPrincipal() instanceof RemoteUserDetails)) {
        throw new InsufficientAuthenticationException(
                "authentication must contain a RemoteUserDetails principal");
    }// w  w w  .  j a v  a 2 s  .  c o  m

    try {
        RemoteUserDetails userDetails = (RemoteUserDetails) authentication.getPrincipal();

        List<GrantedAuthority> authorities = loginWs.authenticate().getRights().stream()
                .map(SimpleGrantedAuthority::new).collect(toList());

        userDetails.setAuthorities(authorities);
        userDetails.setUser(userWs.fetchByLogin(userDetails.getUsername()));

        return new UsernamePasswordAuthenticationToken(userDetails, null, authorities);
    } catch (SOAPFaultException ex) {
        throw new BadCredentialsException(ex.getMessage());
    } catch (CillaServiceException ex) {
        throw new AuthenticationServiceException("couldn't get user details", ex);
    }
}

From source file:com.seajas.search.utilities.spring.security.service.ExtendedAuthenticationProvider.java

/**
 * Override the authenticate method to provide our own extended UserDetails based logic.
 * /*w  w  w .ja  va 2  s.  com*/
 * @param authentication
 * @throws AuthenticationException
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (StringUtils.isEmpty((String) authentication.getPrincipal())
            || StringUtils.isEmpty((String) authentication.getCredentials()))
        throw new BadCredentialsException("The given username / password are invalid");

    UserDetails userDetails = extendedUserDetailsService.getUserDetails((String) authentication.getPrincipal(),
            (String) authentication.getCredentials());

    return new UsernamePasswordAuthenticationToken(userDetails, authentication.getCredentials(),
            userDetails.getAuthorities());
}

From source file:eu.trentorise.smartcampus.ac.provider.filters.SpringAcProvider.java

/**
 * Checks if the authentication token is yet valid
 * /*from  ww w .  j av a  2  s . c  o  m*/
 * @param authentication
 *            spring authentication object
 * @return the authentication object with authenticated flag setted true if
 *         authentication token is yet valid
 * @throws AuthenticationException
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String token = authentication.getPrincipal().toString();
    try {
        boolean valid = WebClient.create(endpointUrl).path("/users/me/validity").header("AUTH_TOKEN", token)
                .accept("application/json").get(Boolean.class);
        if (!valid) {
            throw new BadCredentialsException("Authentication token is absent or expired");
        }
        authentication.setAuthenticated(true);
        return authentication;
    } catch (WebApplicationException e) {
        throw new AuthenticationServiceException("Problem accessing AC provider service: " + e.getMessage());
    }

}

From source file:nl.xillio.gitbreakers.procrastimaster.server.services.UserService.java

public User getUser(Principal principal) {
    if (principal instanceof Authentication) {
        Authentication authentication = (Authentication) principal;
        Object userDetails = authentication.getPrincipal();
        if (userDetails instanceof CustomUserDetails) {
            CustomUserDetails customUserDetails = (CustomUserDetails) userDetails;
            return customUserDetails.user;
        }/*from ww  w .  j a  v a  2  s  . c om*/
    }

    throw new IllegalArgumentException();
}

From source file:com.hp.autonomy.frontend.configuration.authentication.IdolPreAuthenticatedAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final Object principal = authentication.getPrincipal();

    if (principal == null) {
        throw new BadCredentialsException("Principal not supplied");
    }//from   ww w  . ja  v a 2 s  . c  om

    final String username = principal.toString().toLowerCase();

    final UserRoles user = userService.getUser(username, true);

    final Collection<SimpleGrantedAuthority> grantedAuthorities = preAuthenticatedRoles.stream()
            .map(SimpleGrantedAuthority::new).collect(Collectors.toSet());

    final CommunityPrincipal communityPrincipal = new CommunityPrincipal(user.getUid(), username,
            user.getSecurityInfo(), Collections.emptySet());
    final Collection<? extends GrantedAuthority> authorities = authoritiesMapper
            .mapAuthorities(grantedAuthorities);
    return new UsernamePasswordAuthenticationToken(communityPrincipal, null, authorities);
}

From source file:org.emonocot.portal.http.AuthenticatingHttpClientFactory.java

/**
 * @param uri Set the uri/*from  w  ww.j  a  va  2  s  .com*/
 * @param httpMethod set the httpMethod
 * @return a client http request object
 * @throws IOException if there is a problem
 */
public final ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    ClientHttpRequest clientHttpRequest = super.createRequest(uri, httpMethod);
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null && securityContext.getAuthentication() != null) {
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null
                && authentication.getPrincipal().getClass().equals(User.class)) {
            User user = (User) authentication.getPrincipal();
            String unencoded = user.getUsername() + ":" + user.getPassword();
            String encoded = new String(Base64.encodeBase64(unencoded.getBytes()));
            clientHttpRequest.getHeaders().add("Authorization", "Basic " + encoded);
        }
    }
    return clientHttpRequest;
}

From source file:com.caa.koko.auth.CalNetUserDetailService.java

@Override
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
    String calnetUID = (String) token.getPrincipal();

    try {/*from   w  w w. j a v a2s  .  co  m*/
        return lookupCalNetUID(Integer.parseInt(calnetUID));
    } catch (NamingException e) {
        throw new UsernameNotFoundException("LDAP query failed!", e);
    }
}