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:com.acc.storefront.security.impl.DefaultAutoLoginStrategy.java

@Override
public void login(final String username, final String password, final HttpServletRequest request,
        final HttpServletResponse response) {
    final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username,
            password);// w w w. j  a  v  a 2s.  c om
    token.setDetails(new WebAuthenticationDetails(request));
    try {
        final Authentication authentication = getAuthenticationManager().authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        getCustomerFacade().loginSuccess();
        getGuidCookieStrategy().setCookie(request, response);
        getRememberMeServices().loginSuccess(request, response, token);
    } catch (final Exception e) {
        SecurityContextHolder.getContext().setAuthentication(null);
        LOG.error("Failure during autoLogin", e);
    }
}

From source file:org.brekka.pegasus.core.security.UnlockAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(final String token, final UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    Object credentials = authentication.getCredentials();
    String password = credentials.toString();
    if (StringUtils.isBlank(password)) {
        throw new BadCredentialsException("A code is required");
    }//  w w w  . ja  v  a  2 s. co  m

    AnonymousTransferUser anonymousTransferUser = new AnonymousTransferUser(token);
    SecurityContext context = SecurityContextHolder.getContext();

    // Temporarily bind the authentication user to the security context so that we can do the unlock
    // this is primarily for the EventService to capture the IP/remote user.
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(anonymousTransferUser,
            anonymousTransferUser);
    auth.setDetails(authentication.getDetails());
    try {
        context.setAuthentication(auth);
        anonymousService.unlock(token, password, true);
        context.setAuthentication(null);
        return anonymousTransferUser;
    } catch (PhalanxException e) {
        if (e.getErrorCode() == PhalanxErrorCode.CP302) {
            throw new BadCredentialsException("Code appears to be incorrect");
        }
        throw e;
    }
}

From source file:ch.entwine.weblounge.kernel.security.SpringSecurityFormAuthentication.java

/**
 * {@inheritDoc}// www . ja  v a  2s .c o m
 * 
 * @see org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter#attemptAuthentication(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {

    if (postOnly && !"POSTS".equals(request.getMethod())) {
        throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
    }

    // Get the username
    String username = StringUtils.trimToEmpty(request.getParameter(SPRING_SECURITY_FORM_USERNAME_KEY));

    // Get the password
    String password = request.getParameter(SPRING_SECURITY_FORM_PASSWORD_KEY);
    if (password == null) {
        password = "";
    }

    // Using the extracted credentials, create an authentication request
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,
            password);
    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));

    // Place the last username attempted into HttpSession for views
    HttpSession session = request.getSession(false);

    if (session != null || getAllowSessionCreation()) {
        request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY,
                TextEscapeUtils.escapeEntities(username));
    }

    return this.getAuthenticationManager().authenticate(authRequest);
}

From source file:ru.codemine.ccms.api.security.ApiAuthenticationFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse responce = (HttpServletResponse) resp;
    HttpServletRequest request = (HttpServletRequest) req;

    String authToken = request.getHeader("X-Auth-Token");
    String username = apiTokenUtils.getUsernameFromToken(authToken);

    if (username != null) {
        Employee employee = employeeService.getByUsername(username);
        if (apiTokenUtils.validateToken(authToken, employee)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    employee, null, employee.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }/*from   w  w w  . ja va 2  s  . c  om*/
    }

    chain.doFilter(req, resp);
}

From source file:com.ushahidi.swiftriver.core.api.auth.crowdmapid.CrowdmapIDAuthenticationProvider.java

@Transactional(readOnly = true)
@Override//  w w  w. ja  v  a 2  s . c o  m
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = authentication.getName();
    String password = authentication.getCredentials().toString();

    User user = userDao.findByUsernameOrEmail(username);

    if (user == null || !crowdmapIDClient.signIn(username, password)) {
        throw new BadCredentialsException(String.format("Invalid username/password pair for %s", username));
    }
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    for (Role role : user.getRoles()) {
        authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName().toUpperCase()));
    }

    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(username,
            authentication.getCredentials(), authorities);
    result.setDetails(authentication.getDetails());
    return result;
}

From source file:ch.ge.ve.protopoc.jwt.JwtAuthenticationTokenFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader(this.tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }//from ww  w.ja v a  2  s.c  o m
    }

    chain.doFilter(request, response);
}

From source file:com.blackducksoftware.tools.appedit.web.auth.AppEditAuthenticationProvider.java

private UsernamePasswordAuthenticationToken generateAuthenticationToken(String username, String password) {
    AuthenticationResult authResult = authenticateUser(username, password);
    // Grant access
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority(authResult.getRole().name()));
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, password,
            authorities);/*from w  w w .j a  v a  2  s .c om*/
    auth.setDetails(authResult);
    return auth;
}

From source file:com.ram.topup.api.ws.security.filter.AuthenticationTokenProcessingFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(request instanceof HttpServletRequest)) {
        throw new RuntimeException("Expecting a HTTP request");
    }/* w  ww.  ja va 2s  .  co  m*/

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader("X-Auth-Token");

    String userName = TokenUtils.getUserNameFromToken(authToken);

    if (userName != null) {
        UserDetails userDetails = this.userService.loadUserByUsername(userName);
        if (TokenUtils.validateToken(authToken, userDetails.getUsername(), userDetails.getPassword())) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(
                    new WebAuthenticationDetailsSource().buildDetails((HttpServletRequest) request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);
}

From source file:org.ff4j.security.test.FlipSecurityTests.java

@Before
public void setUp() throws Exception {
    securityCtx = SecurityContextHolder.getContext();
    // Init SpringSecurity Context
    SecurityContext context = new SecurityContextImpl();
    List<GrantedAuthority> listOfRoles = new ArrayList<GrantedAuthority>();
    listOfRoles.add(new SimpleGrantedAuthority("ROLE_USER"));
    User u1 = new User("user1", "user1", true, true, true, true, listOfRoles);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u1.getUsername(),
            u1.getPassword(), u1.getAuthorities());
    token.setDetails(u1);
    context.setAuthentication(token);//from w w w.  ja v a 2 s .c om
    SecurityContextHolder.setContext(context);
    // <--

    ff4j = new FF4j("test-ff4j-security-spring.xml");
    ff4j.setAuthorizationsManager(new SpringSecurityAuthorisationManager());
}

From source file:com.javiermoreno.springboot.rest.AuthenticationTokenProcessingFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    String encryptedToken = request.getHeader("X-Auth-Token");
    if (SecurityContextHolder.getContext().getAuthentication() == null && encryptedToken != null) {
        Token token = new Token(cryptoService, encryptedToken);
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null) {
            ip = request.getRemoteAddr();
        }/*  w ww.  j a v a2 s.  c  om*/
        if (ip.equals(token.getIp()) == true && token.isExpired() == false) {
            UserDetails userDetails = userDetailsService.loadUserByUsername(token.getUsername());
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails.getUsername(), userDetails.getPassword());
            authentication.setDetails(
                    new WebAuthenticationDetailsSource().buildDetails((HttpServletRequest) request));
            SecurityContextHolder.getContext()
                    .setAuthentication(authenticationManager.authenticate(authentication));
        }

    }
    chain.doFilter(req, res);
}