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.trenako.security.SpringSecurityService.java

@Override
public AccountDetails getCurrentUser() {
    Authentication auth = getSecContext().getAuthentication();
    return auth == null ? null : (AccountDetails) auth.getPrincipal();
}

From source file:com.porvak.bracket.social.develop.oauth.OAuthSessionManagerProviderTokenServices.java

public void authorizeRequestToken(String requestToken, String verifier, Authentication authentication) {
    if (!(authentication.getPrincipal() instanceof Account)) {
        throw new IllegalArgumentException("Authenticated user principal is not of expected Account type");
    }/* www .j av  a2 s  .c  o  m*/
    try {
        Long authorizingAccountId = Long.valueOf(((Account) authentication.getPrincipal()).getId());
        sessionManager.authorize(requestToken, authorizingAccountId, verifier);
    } catch (InvalidRequestTokenException e) {
        throw new InvalidOAuthTokenException(e.getMessage());
    }
}

From source file:fr.mael.microrss.util.SecurityUtil.java

public User getCurrentUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Object principal = auth.getPrincipal();
    if (principal == null || !(principal instanceof User)) {
        throw new AccessDeniedException("User is not logged in");
    }/*from   w ww . j a v  a  2  s  . c o m*/
    User user = (User) principal;
    //TODO the id is lost when the server is restarted
    //will it happen in production ?
    if (user.getId() == null) {
        throw new AccessDeniedException("User is not logged in");
    }

    return user;
}

From source file:org.smigo.user.authentication.EmptyAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    log.info("Successful authentication by " + authentication.getPrincipal());
}

From source file:csns.security.AuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    User user = (User) authentication.getPrincipal();
    logger.info(user.getUsername() + " signed in from " + request.getRemoteAddr());

    RequestCache requestCache = new HttpSessionRequestCache();
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null) {
        super.onAuthenticationSuccess(request, response, authentication);
        return;//from w  w  w .j  a  v a  2 s  . co  m
    }

    getRedirectStrategy().sendRedirect(request, response, defaultUrls.userHomeUrl(request));
}

From source file:bookpub.security.LogoutRedirectHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    User user = (User) authentication.getPrincipal();
    logger.info(user.getUsername() + " signed out.");

    if (request.getParameter("mobile") != null)
        objectMapper.writeValue(response.getWriter(), new ServiceResponse());
    else {//from w w w . j a va2 s .  co m
        SimpleUrlLogoutSuccessHandler logoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
        logoutSuccessHandler.setDefaultTargetUrl("/");
        logoutSuccessHandler.onLogoutSuccess(request, response, authentication);
    }
}

From source file:ch.wisv.areafiftylan.products.controller.TicketRestController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/teammembers", method = RequestMethod.GET)
public Collection<Ticket> getTicketsFromTeamMembers(Authentication auth) {
    User u = (User) auth.getPrincipal();

    return ticketService.getOwnedTicketsAndFromTeamMembers(u);
}

From source file:ch.wisv.areafiftylan.users.controller.UserProfileRestController.java

/**
 * Add a profile to the current user. An empty profile is created when a user is created, so this method fills the
 * existing fields//  w w w  .  ja  v a 2s  . com
 *
 * @param input A representation of the profile
 *
 * @return The user with the new profile
 */
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/current/profile", method = RequestMethod.POST)
public ResponseEntity<?> addProfile(@Validated @RequestBody ProfileDTO input, Authentication auth) {
    return this.addProfile(((User) auth.getPrincipal()).getId(), input);
}

From source file:edu.csula.squirrels.security.LogoutRedirectHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    User user = (User) authentication.getPrincipal();
    logger.info(user.getUsername() + " signed out.");

    if (request.getParameter("mobile") != null)
        objectMapper.writeValue(response.getWriter(), new ServiceResponse("loggedOut"));
    else {/*w  w  w.jav  a  2s.c  o m*/
        SimpleUrlLogoutSuccessHandler logoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
        logoutSuccessHandler.setDefaultTargetUrl("/");
        logoutSuccessHandler.onLogoutSuccess(request, response, authentication);
    }
}

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

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    User currentUser = (User) authentication.getPrincipal();
    logger.info("[{}] Successfully authenticated user from IP {}", currentUser,
            SecurityHelper.getClientIpAddr(request));
    clearAuthenticationAttributes(request);
    if (!response.isCommitted()) {
        Settings settings = Settings.loadSettings(settingsRepository);
        response.setHeader("System-ID", settings.getId());
        response.setHeader("System-Version", settings.getSystemVersion());
        response.setHeader("User-ID", currentUser.getId());
        response.sendError(HttpServletResponse.SC_OK, "Successfully logged in");
    }//  ww  w.  j av  a 2  s .c o  m
}