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:eu.freme.broker.security.TokenAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    @SuppressWarnings("unchecked")
    Optional<String> token = (Optional<String>) authentication.getPrincipal();
    if (!token.isPresent() || token.get().isEmpty()) {
        throw new BadCredentialsException("Invalid token");
    }//from www. ja  va2  s  .com

    Token tokenObject = tokenService.retrieve(token.get());
    if (tokenObject == null) {
        throw new BadCredentialsException("Invalid token");
    }

    tokenService.updateLastUsed(tokenObject);

    User user = tokenObject.getUser();
    return new AuthenticationWithToken(user, null,
            AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRole()), tokenObject);
}

From source file:org.cloudfoundry.identity.uaa.security.DefaultSecurityContextAccessor.java

@Override
public String getUserId() {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    return a == null ? null : ((UaaPrincipal) a.getPrincipal()).getId();
}

From source file:org.exoplatform.acceptance.security.CrowdAuthenticationProviderWrapper.java

/**
 * {@inheritDoc}/*from  ww w.  j a v a  2  s  .  c  o m*/
 * Performs authentication with the same contract as {@link
 * org.springframework.security.authentication.AuthenticationManager#authenticate(Authentication)}.
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Authentication crowdAuthentication = crowdAuthenticationProvider.authenticate(authentication);
    return new UsernamePasswordAuthenticationToken(crowdAuthentication.getPrincipal(),
            crowdAuthentication.getCredentials(),
            grantedAuthoritiesMapper.mapAuthorities(crowdAuthentication.getAuthorities()));
}

From source file:eu.trentorise.smartcampus.aac.conf.OAuthAuthenticationProvider.java

/**
 * Check that the token is not empty, validate against the {@link TokenStore} if specified,
 * and if it is valid for the given scope (if specified)
 *//*from   w w  w. j  ava  2 s.c  o m*/
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String token = (String) authentication.getPrincipal();
    if (token == null || token.trim().isEmpty()) {
        throw new BadCredentialsException("Authentication token is absent");
    }
    if (tokenStore != null && !tokenStore.validateToken(token)) {
        throw new BadCredentialsException("Authentication token is not valid");
    }
    try {
        if (scope != null && aacURL != null
                && !new AACService(aacURL, null, null).isTokenApplicable(token, scope)) {
            throw new BadCredentialsException("Authentication token is not valid for the required scope");
        }
    } catch (AACException e) {
        throw new BadCredentialsException("Failed to valdiate token scope: " + e.getMessage());
    }
    authentication.setAuthenticated(true);
    return authentication;
}

From source file:de.thm.arsnova.controller.SecurityExceptionControllerAdvice.java

@ExceptionHandler(AccessDeniedException.class)
public void handleAccessDeniedException(final Exception e, final HttpServletRequest request,
        final HttpServletResponse response) {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || authentication.getPrincipal() == null
            || authentication instanceof AnonymousAuthenticationToken) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return;/*from w ww .jav  a 2 s. c o  m*/
    }
    response.setStatus(HttpStatus.FORBIDDEN.value());
}

From source file:org.davidmendoza.esu.utils.LoginHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    super.onAuthenticationSuccess(request, response, authentication);
    String username = ((UserDetails) authentication.getPrincipal()).getUsername();
    log.info("El usuario {} ha entrado.", username);
}

From source file:org.dataconservancy.ui.api.BaseController.java

public Person getAuthenticatedUser() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication authn = ctx.getAuthentication();
    if (authn != null && authn.getPrincipal() != null && authn.getPrincipal() instanceof Person) {
        Person loggedInUser = (Person) authn.getPrincipal();

        if (loggedInUser != null && userService != null) {
            return userService.get(loggedInUser.getId());
        }/*  w  w w . j  ava  2  s . c o  m*/

        return loggedInUser;
    }
    return null;
}

From source file:de.hska.ld.core.config.security.AjaxAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    String userJson = null;/* w  ww. ja va 2  s  . co  m*/
    try {
        userJson = mapper.writeValueAsString(authentication.getPrincipal());
    } catch (JsonProcessingException e) {
        LOGGER.error(e.getMessage());
    }

    if (userJson != null) {
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(userJson);
        response.setStatus(HttpServletResponse.SC_OK);
    } else {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.openlmis.fulfillment.security.UserNameProvider.java

@Override
public String provide() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (null == auth) {
        return "unauthenticated user";
    }//  www  .  j  a  v  a  2  s . c o  m

    Object principal = auth.getPrincipal();
    if (null == principal) {
        return "unknown user";
    }

    return principal.toString();
}

From source file:io.github.autsia.crowly.security.CrowlyAuthenticationManager.java

public String getCurrentUserEmail() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return (String) authentication.getPrincipal();
}