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:net.firejack.platform.web.security.spring.AuthenticationManager.java

/**
 * @param user/*from  w w  w .j  av a 2  s.  c om*/
 * @param session
 * @param roles
 * @throws org.springframework.security.core.AuthenticationException
 *
 */
public void doAuthentication(IUserInfoProvider user, HttpSession session,
        SpringSecurityAuthenticationRoleType... roles) throws AuthenticationException {
    if (user != null) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        for (SpringSecurityAuthenticationRoleType role : roles) {
            authorities.add(new GrantedAuthorityImpl(role.name()));
        }
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
                user.getPassword(), authorities);
        token.setDetails(user);
        sessionManager.addUserToSession(user, session);

        SecurityContextHolder.getContext().setAuthentication(token);
        OpenFlamePrincipal fjkPrincipal = new UserPrincipal(user);
        OPFContext.initContext(fjkPrincipal);
    }
}

From source file:com.vivastream.security.oauth2.provider.DynamoDBUserDetailsManager.java

protected Authentication createNewAuthentication(UserDetails user, Authentication currentAuth,
        String newPassword) {//from  w  w  w. j  av a2 s.  c  om

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

    return newAuthentication;
}

From source file:fr.xebia.springframework.security.core.providers.ExtendedDaoAuthenticationProviderTest.java

private void testAdditionalchecks(String allowedRemoteAddresses, String remoteAddr) {
    ExtendedDaoAuthenticationProvider daoAuthenticationProvider = new ExtendedDaoAuthenticationProvider();

    Collection<GrantedAuthority> grantedAuthorities = Collections.emptyList();

    ExtendedUser extendedUser = new ExtendedUser("test-user", "test-password", true, true, true, true,
            grantedAuthorities);/*from   ww w.j  ava2s  .c o  m*/

    extendedUser.setAllowedRemoteAddresses(allowedRemoteAddresses);

    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("test-user",
            "test-password");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr(remoteAddr);
    authentication.setDetails(new WebAuthenticationDetails(request));

    daoAuthenticationProvider.additionalAuthenticationChecks(extendedUser, authentication);
}

From source file:org.saiku.web.service.SessionService.java

public void authenticate(HttpServletRequest req, String username, String password) {
    try {/*from w  w w  .  j a  va2s  . c o  m*/
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
        token.setDetails(new WebAuthenticationDetails(req));
        Authentication authentication = this.authenticationManager.authenticate(token);
        log.debug("Logging in with [{}]", authentication.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(authentication);
    } catch (BadCredentialsException bd) {
        throw new RuntimeException("Authentication failed for: " + username, bd);
    }

}

From source file:org.openmrs.contrib.metadatarepository.webapp.controller.SignupController.java

@RequestMapping(method = RequestMethod.POST)
public String onSubmit(User user, BindingResult errors, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (request.getParameter("cancel") != null) {
        return getCancelView();
    }// w  w w.  j a va2s.  co m

    if (log.isDebugEnabled()) {
        log.debug("entering 'onSubmit' method...");
    }
    Locale locale = request.getLocale();

    user.setEnabled(true);

    // Set the default user role on this new user
    user.addRole(roleManager.getRole(Constants.USER_ROLE));

    try {
        this.getUserManager().saveUser(user);
    } catch (AccessDeniedException ade) {
        // thrown by UserSecurityAdvice configured in aop:advisor userManagerSecurity
        log.warn(ade.getMessage());
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return null;
    } catch (UserExistsException e) {
        errors.rejectValue("username", "errors.existing.user",
                new Object[] { user.getUsername(), user.getEmail() }, "duplicate user");

        // redisplay the unencrypted passwords
        user.setPassword(user.getConfirmPassword());
        return "signup";
    }

    saveMessage(request, getText("user.registered", user.getUsername(), locale));
    request.getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE);

    // log user in automatically
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user.getUsername(),
            user.getConfirmPassword(), user.getAuthorities());
    auth.setDetails(user);
    SecurityContextHolder.getContext().setAuthentication(auth);

    // Send user an e-mail
    if (log.isDebugEnabled()) {
        log.debug("Sending user '" + user.getUsername() + "' an account information e-mail");
    }

    // Send an account information e-mail
    message.setSubject(getText("signup.email.subject", locale));

    try {
        sendUserMessage(user, getText("signup.email.message", locale), RequestUtil.getAppURL(request));
    } catch (MailException me) {
        saveError(request, me.getMostSpecificCause().getMessage());
    }

    return getSuccessView();
}

From source file:com.katsu.springframework.security.authentication.HtmlAuthenticationProvider.java

private Authentication createSuccessAuthentication(AbstractAuthenticationToken upat,
        Collection<? extends GrantedAuthority> roles) {
    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(upat.getPrincipal(),
            upat.getCredentials(), roles);
    result.setDetails(upat.getDetails());
    return result;
}

From source file:com.gm.machine.web.IndexAction.java

public String login() throws Exception {
    try {//from w  w  w  .jav a2s. com
        HttpServletRequest request = Struts2Utils.getRequest();
        User entity = userManager.getUserByUsername(request.getParameter("username"));
        if (entity == null) {
            mess = "???";
            return "login";
        } else {
            if (entity.isUserlock()) {
                mess = "???";
                return "login";
            }
        }

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                request.getParameter("username"), request.getParameter("loginpwd"));
        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser = authenticationManager.authenticate(token);

        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
        request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
                SecurityContextHolder.getContext());

        entity.setLastLoginDate(new Date());// ?
        entity.setLoginTimes(entity.getLoginTimes() + 1);// 
        userManager.save(entity);
        request.getSession().setAttribute("loginuser", entity);
        request.getSession().setAttribute("userid", entity.getId());
        mess = "?";
        return "login";
    } catch (AuthenticationException e) {
        mess = "?";
        e.printStackTrace();
        return "login";

    }
}

From source file:ru.org.linux.auth.LoginController.java

@RequestMapping(value = "/login_process", method = RequestMethod.POST)
public ModelAndView loginProcess(@RequestParam("nick") final String username,
        @RequestParam("passwd") final String password, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    try {//from   w  w  w  .ja  v  a  2s.c o m
        UserDetailsImpl details = (UserDetailsImpl) userDetailsService.loadUserByUsername(username);
        token.setDetails(details);
        Authentication auth = authenticationManager.authenticate(token);
        UserDetailsImpl userDetails = (UserDetailsImpl) auth.getDetails();
        if (!userDetails.getUser().isActivated()) {
            throw new AccessViolationException("User not activated");
        }
        SecurityContextHolder.getContext().setAuthentication(auth);
        rememberMeServices.loginSuccess(request, response, auth);
        AuthUtil.updateLastLogin(auth, userDao);
    } catch (Exception e) {
        return new ModelAndView(new RedirectView("/login.jsp?error=true"));
    }
    return new ModelAndView(new RedirectView("/"));
}