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:com.changeme.security.SecurityUtils.java

/**
 * Configures the Spring Security {@link SecurityContext} to be authenticated as the user with the given username and
 * password as well as the given granted authorities.
 * //from www. j  ava 2s  .  c  om
 * @param username must not be {@literal null} or empty.
 * @param password must not be {@literal null} or empty.
 * @param roles
 */
public static void runAs(String username, String password, String... roles) {

    Assert.notNull(username, "Username must not be null!");
    Assert.notNull(password, "Password must not be null!");

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(username,
            password, AuthorityUtils.createAuthorityList(roles)));
}

From source file:com.springsource.greenhouse.account.AccountUtils.java

/**
 * Construct a Spring Security Authentication token from an Account object.
 * Useful for treating the Account as a Principal in Spring Security.
 *///  w w  w . jav  a2s .c  om
public static Authentication authenticationTokenFor(Account account) {
    return new UsernamePasswordAuthenticationToken(account, null, (Collection<GrantedAuthority>) null);
}

From source file:com.indusborn.util.AccountUtils.java

/**
 * Construct a Spring Security Authentication token from an Account object.
 * Useful for treating the Account as a Principal in Spring Security.
 */// w w w  .  j  a v  a  2s . c  o m
public static Authentication authenticationTokenFor(UserVO userVO) {
    return new UsernamePasswordAuthenticationToken(userVO, null, (Collection<GrantedAuthority>) null);
}

From source file:com.clz.share.sec.util.SignInUtils.java

private static Principal signinPrivate(String userId) {
    Authentication authentication = new UsernamePasswordAuthenticationToken("user", "pass",
            AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return (Principal) authentication.getPrincipal();
}

From source file:org.runway.utils.AuthenticationUtils.java

public static void autoLogin(User user, HttpServletRequest request,
        AuthenticationManager authenticationManager) {

    //           GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
    //             user.getAuthority()) };

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
            user.getPassword(), user.getAuthorities());

    // generate session if one doesn't exist
    HttpSession session = request.getSession();

    token.setDetails(new WebAuthenticationDetails(request));
    Authentication authenticatedUser = authenticationManager.authenticate(token);

    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    // setting role to the session
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());

}

From source file:com.github.persapiens.jsfboot.security.AuthenticationFactory.java

public static Authentication authentication(String... authorities) {
    return new UsernamePasswordAuthenticationToken("user", "user", grantedAuthorities(authorities));
}

From source file:com.folion.social.SignInUtils.java

public static void signIn(Account user) {

    List<GrantedAuthority> grantedAuthorities = createAuthorities(user.getAccountRole());
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken(user.getEmail(), null, grantedAuthorities));
}

From source file:com.pluralsight.controller.test.AbstractSecurityTest.java

protected void login(String customUsername) {
    UserDetails ud = userDetailsService.loadUserByUsername(customUsername);
    Authentication auth = new UsernamePasswordAuthenticationToken(ud, ud.getPassword(), ud.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
}

From source file:com.mycompany.thymeleafspringapp.service.SimpleSigninAdapter.java

@Override
public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {
    String email = connection.fetchUserProfile().getEmail();
    UserDetails userDetails = userService.loadUserByUsername(email);
    SecurityContextHolder.getContext()//from   w  w  w .  j  av  a 2  s  . co m
            .setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, null));
    return null;
}

From source file:com.github.carlomicieli.nerdmovies.security.SpringSecurityService.java

@Override
public void authenticate(MailUser user) {
    MailUserDetails det = new MailUserDetails(user);
    Authentication authentication = new UsernamePasswordAuthenticationToken(det, det.getPassword(),
            det.getAuthorities());/*from w w w . ja va  2  s. c o m*/
    SecurityContextHolder.getContext().setAuthentication(authentication);
}