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

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

Introduction

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

Prototype

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
        Collection<? extends GrantedAuthority> authorities) 

Source Link

Document

This constructor should only be used by AuthenticationManager or AuthenticationProvider implementations that are satisfied with producing a trusted (i.e.

Usage

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

/**
 * {@inheritDoc}// w w  w.  java 2 s. c om
 * 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:org.cloudfoundry.identity.uaa.social.SocialClientAuthenticationFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {
    SocialClientUserDetails user = socialClientUserDetailsSource.getUserDetails();
    Collection<GrantedAuthority> authorities = user.getAuthorities();
    UsernamePasswordAuthenticationToken result;
    if (authorities != null && !authorities.isEmpty()) { // TODO: correlate user data with existing accounts if email or username missing
        result = new UsernamePasswordAuthenticationToken(user, null, authorities);
    } else {//  ww  w .ja  v  a 2  s .c  o  m
        // Unauthenticated
        result = new UsernamePasswordAuthenticationToken(user, null);
    }
    result.setDetails(authenticationDetailsSource.buildDetails(request));
    return result;
}

From source file:cn.net.withub.demo.bootsec.hello.security.CustomAuthenticationProvider.java

@Transactional
@Override/* www . j  a va 2s  .  c  o m*/
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
    String username = token.getName(); //???
    //?
    UserDetails userDetails = null;
    if (username != null) {
        userDetails = userDetailsService.loadUserByUsername(username);
    }

    if (userDetails == null) {
        return null;//null??
        //throw new UsernameNotFoundException("??/?");
    } else if (!userDetails.isEnabled()) {
        throw new DisabledException("?");
    } else if (!userDetails.isAccountNonExpired()) {
        throw new AccountExpiredException("?");
    } else if (!userDetails.isAccountNonLocked()) {
        throw new LockedException("??");
    } else if (!userDetails.isCredentialsNonExpired()) {
        throw new LockedException("?");
    }

    //??
    String encPass = userDetails.getPassword();

    //authentication?credentials
    if (!md5PasswordEncoder.isPasswordValid(encPass, token.getCredentials().toString(), null)) {
        throw new BadCredentialsException("Invalid username/password");
    }

    //?
    return new UsernamePasswordAuthenticationToken(userDetails, encPass, userDetails.getAuthorities());
}

From source file:org.statefulj.demo.ddd.customer.domain.impl.CustomerSessionServiceImpl.java

@Override
public void login(HttpSession session, Customer customer) {
    UserDetails customerDetails = this.getDetails(customer);
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(customerDetails,
            customer.getPassword(), customerDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());
}

From source file:org.oncoblocks.centromere.web.security.AuthenticationTokenProcessingFilter.java

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

    if (!(request instanceof HttpServletRequest)) {
        throw new RuntimeException("Expecting an HTTP request.");
    }/*w w  w. j a  v  a2 s  .c o m*/
    HttpServletRequest httpRequest = (HttpServletRequest) request;

    String authToken = httpRequest.getHeader("X-Auth-Token");
    if (authToken == null) {
        authToken = httpRequest.getParameter("token");
    }

    String username = tokenOperations.getUserNameFromToken(authToken);

    if (username != null) {
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (tokenOperations.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    chain.doFilter(request, response);

}

From source file:org.socialsignin.exfmproxy.mvc.workaround.auth.WorkaroundUsernamePasswordAuthenticationFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException {

    Authentication a = super.attemptAuthentication(request, response);
    String[] usernameAndPassword = ((User) a.getPrincipal()).getUsername().split(USERNAME_PASSWORD_DELIMITER);
    User user = new User(usernameAndPassword[0], usernameAndPassword[1], a.getAuthorities());
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user,
            usernameAndPassword[1], a.getAuthorities());
    setDetails(request, authentication);
    return authentication;

}

From source file:uk.co.threeonefour.ifictionary.web.user.service.DaoUserService.java

public void logInUser(User userEntity) {

    org.springframework.security.core.userdetails.User springUser = buildUserFromUserEntity(userEntity);
    Authentication authentication = new UsernamePasswordAuthenticationToken(springUser, null,
            springUser.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:org.n52.oss.ui.services.OSSAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
    String username = arg0.getName();
    String password = arg0.getCredentials().toString();

    AuthToken token = authenticateOSS(username, password);

    if (token.auth_token != null) {
        if (!token.isValid)
            throw new UsernameNotFoundException(
                    "Username is not validated please contact site administration!");

        final List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_SCRIPT_AUTHOR"));

        if (token.isAdmin)
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        final UserDetails principal = new User(username, token.auth_token, grantedAuths);
        final Authentication auth = new UsernamePasswordAuthenticationToken(principal, token.auth_token,
                grantedAuths);//w w w.  j  a  v  a2 s .co m
        return auth;

    } else
        throw new UsernameNotFoundException("Wrong username/password combination");
}

From source file:org.ng200.openolympus.TestUtilities.java

public void logInAsAdmin() {
    final SecurityContext context = SecurityContextHolder.createEmptyContext();

    final User principal = this.userService.getUserByUsername("admin");
    final Authentication auth = new UsernamePasswordAuthenticationToken(principal, "admin",
            principal.getAuthorities());
    context.setAuthentication(auth);//from  w ww .  ja va2 s . co  m
    SecurityContextHolder.setContext(context);
}

From source file:org.osiam.security.authorization.AccessTokenValidationService.java

@Override
public OAuth2Authentication loadAuthentication(String token) {
    AccessToken accessToken = validateAccessToken(token);

    Set<String> scopes = new HashSet<String>();
    if (accessToken.getScopes() != null) {
        for (Scope scope : accessToken.getScopes()) {
            scopes.add(scope.toString());
        }//from  w  w  w. j  a  v a2  s . c  o m
    }

    DefaultAuthorizationRequest authrequest = new DefaultAuthorizationRequest(accessToken.getClientId(),
            scopes);
    authrequest.setApproved(true);

    Authentication auth = null;

    if (!accessToken.isClientOnly()) {
        User authUser = new User.Builder(accessToken.getUserName()).setId(accessToken.getUserId()).build();

        auth = new UsernamePasswordAuthenticationToken(authUser, null, new ArrayList<GrantedAuthority>());
    }

    return new OAuth2Authentication(authrequest, auth);
}