Example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken setDetails

List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken setDetails

Introduction

In this page you can find the example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken setDetails.

Prototype

public void setDetails(Object details) 

Source Link

Usage

From source file:org.training.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-email", method = RequestMethod.POST)
@RequireHardLogIn//  ww  w.  j a  v  a  2s  . com
public String updateEmail(final UpdateEmailForm updateEmailForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getEmailValidator().validate(updateEmailForm, bindingResult);
    String returnAction = REDIRECT_TO_UPDATE_EMAIL_PAGE;

    if (!bindingResult.hasErrors() && !updateEmailForm.getEmail().equals(updateEmailForm.getChkEmail())) {
        bindingResult.rejectValue("chkEmail", "validation.checkEmail.equals", new Object[] {},
                "validation.checkEmail.equals");
    }

    if (bindingResult.hasErrors()) {
        returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
    } else {
        try {
            customCustomerFacade.changeUid(updateEmailForm.getEmail(), updateEmailForm.getPassword());
            GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                    "text.account.profile.confirmationUpdated", null);

            // Replace the spring security authentication with the new UID
            final String newUid = customCustomerFacade.getCurrentCustomer().getUid().toLowerCase();
            final Authentication oldAuthentication = SecurityContextHolder.getContext().getAuthentication();
            final UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
                    newUid, null, oldAuthentication.getAuthorities());
            newAuthentication.setDetails(oldAuthentication.getDetails());
            SecurityContextHolder.getContext().setAuthentication(newAuthentication);
        } catch (final DuplicateUidException e) {
            bindingResult.rejectValue("email", "profile.email.unique");
            returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
        } catch (final PasswordMismatchException passwordMismatchException) {
            bindingResult.rejectValue("password", PROFILE_CURRENT_PASSWORD_INVALID);
            returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
        }
    }

    return returnAction;
}

From source file:org.unitedinternet.cosmo.security.impl.CosmoSecurityManagerImpl.java

/**
 * Initiate the current security context with the current user.
 * This method is used when the server needs to run code as a
 * specific user./*from  w  w  w . java2s .  co  m*/
 */
public CosmoSecurityContext initiateSecurityContext(User user) throws CosmoSecurityException {

    UserDetails details = new CosmoUserDetails(user);

    UsernamePasswordAuthenticationToken credentials = new UsernamePasswordAuthenticationToken(details, "",
            details.getAuthorities());

    credentials.setDetails(details);
    SecurityContext sc = SecurityContextHolder.getContext();
    sc.setAuthentication(credentials);
    return createSecurityContext(credentials);
}

From source file:pl.bcichecki.rms.customizations.org.springframework.security.web.authentication.www.EventPublisherAwareDigestAuthenticationFilter.java

private Authentication createSuccessfulAuthentication(HttpServletRequest request, UserDetails user) {
    UsernamePasswordAuthenticationToken authRequest;
    if (createAuthenticatedToken) {
        authRequest = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
    } else {/*from  w  ww.  j a  v a2s. c  o  m*/
        authRequest = new UsernamePasswordAuthenticationToken(user, user.getPassword());
    }

    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));

    return authRequest;
}

From source file:ro.nextreports.server.web.integration.IntegrationAuthenticationFilter.java

protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
}

From source file:ru.org.linux.user.EditRegisterController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView edit(HttpServletRequest request, HttpServletResponse response,
        @Valid @ModelAttribute("form") EditRegisterRequest form, Errors errors) throws Exception {
    Template tmpl = Template.getTemplate(request);

    if (!tmpl.isSessionAuthorized()) {
        throw new AccessViolationException("Not authorized");
    }//from   w w  w. j a v a 2 s .  com

    String nick = tmpl.getNick();
    String password = Strings.emptyToNull(form.getPassword());

    if (password != null && password.equalsIgnoreCase(nick)) {
        errors.reject(null, "   ? ? ");
    }

    InternetAddress mail = null;

    if (!Strings.isNullOrEmpty(form.getEmail())) {
        try {
            mail = new InternetAddress(form.getEmail());
        } catch (AddressException e) {
            errors.rejectValue("email", null, "? e-mail: " + e.getMessage());
        }
    }

    String url = null;

    if (!Strings.isNullOrEmpty(form.getUrl())) {
        url = URLUtil.fixURL(form.getUrl());
    }

    String name = Strings.emptyToNull(form.getName());

    if (name != null) {
        name = StringUtil.escapeHtml(name);
    }

    String town = null;

    if (!Strings.isNullOrEmpty(form.getTown())) {
        town = StringUtil.escapeHtml(form.getTown());
    }

    String info = null;

    if (!Strings.isNullOrEmpty(form.getInfo())) {
        info = StringUtil.escapeHtml(form.getInfo());
    }

    ipBlockDao.checkBlockIP(request.getRemoteAddr(), errors, tmpl.getCurrentUser());

    boolean emailChanged = false;

    User user = userService.getUser(nick);

    if (Strings.isNullOrEmpty(form.getOldpass())) {
        errors.rejectValue("oldpass", null,
                "? ? ?   ");
    } else if (!user.matchPassword(form.getOldpass())) {
        errors.rejectValue("oldpass", null, "? ");
    }

    user.checkAnonymous();

    String newEmail = null;

    if (mail != null) {
        if (user.getEmail() != null && user.getEmail().equals(form.getEmail())) {
            newEmail = null;
        } else {
            if (userDao.getByEmail(mail.getAddress().toLowerCase(), false) != null) {
                errors.rejectValue("email", null, " email  ???");
            }

            newEmail = mail.getAddress().toLowerCase();

            emailChanged = true;
        }
    }

    if (!errors.hasErrors()) {
        userDao.updateUser(user, name, url, newEmail, town, password, info);
        //  token-  ? ? ?
        if (password != null) {
            try {
                UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                        user.getNick(), password);
                UserDetailsImpl details = (UserDetailsImpl) userDetailsService
                        .loadUserByUsername(user.getNick());
                token.setDetails(details);
                Authentication auth = authenticationManager.authenticate(token);
                SecurityContextHolder.getContext().setAuthentication(auth);
                rememberMeServices.loginSuccess(request, response, auth);
            } catch (Exception ex) {
                logger.error(
                        " ? ?    ? ?. ",
                        ex);
            }
        }

        if (emailChanged) {
            emailService.sendEmail(user.getNick(), newEmail, false);
        }
    } else {
        return new ModelAndView("edit-reg");
    }

    if (emailChanged) {
        String msg = " ?  ?. "
                + " ?  " + StringUtil.escapeHtml(newEmail)
                + " ?   ? email.";

        return new ModelAndView("action-done", "message", msg);
    } else {
        return new ModelAndView(new RedirectView("/people/" + tmpl.getNick() + "/profile"));
    }
}

From source file:ubc.pavlab.aspiredb.server.security.authentication.UserManagerImpl.java

protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}

From source file:ubic.gemma.core.security.authentication.UserManagerImpl.java

private Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = this.loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}

From source file:ubic.gemma.security.authentication.UserManagerImpl.java

protected Authentication createNewAuthentication(Authentication currentAuth,
        @SuppressWarnings("unused") String newPassword) {
    UserDetails user = loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}