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:gateway.auth.PiazzaBasicAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    try {// w  w w  . j  a va 2s .  co m
        AuthenticationResponse response = userDetails.getAuthenticationDecision(authentication.getName());
        if (response.getAuthenticated()) {
            return new UsernamePasswordAuthenticationToken(response.getUsername(), null, new ArrayList<>());
        }
    } catch (Exception exception) {
        String error = String.format("Error retrieving UUID: %s.", exception.getMessage());
        logger.log(error, PiazzaLogger.ERROR);
        LOGGER.error(error, exception);
    }
    return null;
}

From source file:com.yatrix.activity.service.signin.utils.SimpleSignInAdapter.java

public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {

    UserAccount account = userAdminService.loadUserByUserId(localUserId);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(localUserId,
            account.getPassword(), account.getAuthorities()));
    System.out.println("USERID = " + localUserId);
    return extractOriginalUrl(request);
}

From source file:org.openvpms.component.business.service.security.MemorySecurityServiceTestCase.java

/**
 * Create a secure context so that we can do some authorization testing.
 *
 * @param user        the user name/*  ww  w. j  a  v  a 2  s .co  m*/
 * @param password    the password
 * @param authorities the authorities of the person
 */
protected void createSecurityContext(String user, String password, String... authorities) {
    List<GrantedAuthority> granted = new ArrayList<GrantedAuthority>();
    for (String authority : authorities) {
        granted.add(new ArchetypeAwareGrantedAuthority(authority));
    }
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password,
            granted);
    SecurityContextHolder.getContext().setAuthentication(token);
}

From source file:org.opentides.util.SecurityUtilTest.java

@Test
public void testGetSessionUser() {
    List<GrantedAuthority> auths = new ArrayList<>();
    auths.add(new SimpleGrantedAuthority("ROLE1"));
    auths.add(new SimpleGrantedAuthority("ROLE2"));

    UserDetails userDetails = new User("admin", "password", auths);
    SessionUser sessionUser = new SessionUser(userDetails);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(sessionUser,
            null, auths);/*from  w w w  .j  av a  2s.c om*/
    SecurityContextHolder.getContext().setAuthentication(authentication);

    SessionUser actual = SecurityUtil.getSessionUser();
    assertNotNull(actual);
    assertEquals("admin", actual.getUsername());
}

From source file:com.mec.Security.JWTLoginFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
        throws AuthenticationException, IOException, ServletException {
    //System.out.println("JWTLoginFilter - attemptAuthentication");
    AccountCredentials creds = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class);//no permite inner class
    return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(creds.getUsername(),
            creds.getPassword(), Collections.emptyList()));
}

From source file:org.homiefund.test.config.SecurityConextPrincipalFactory.java

@Override
public SecurityContext createSecurityContext(WithUser withUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    UserDTO user = new UserDTO();
    user.setId(withUser.id());/*from ww w  . ja v  a2  s  . c  om*/
    user.setHomes(Stream.of(withUser.homes()).map(h -> {
        HomeDTO home = new HomeDTO();
        home.setId(h.id());
        return home;
    }).collect(Collectors.toList()));

    Authentication auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
            user.getAuthorities());

    context.setAuthentication(auth);

    return context;
}

From source file:com.apress.prospringintegration.security.SecurityMain.java

@SuppressWarnings("unchecked")
public static SecurityContext createContext(String username, String password, String... roles) {
    SecurityContextImpl ctxImpl = new SecurityContextImpl();
    UsernamePasswordAuthenticationToken authToken;
    if (roles != null && roles.length > 0) {
        GrantedAuthority[] authorities = new GrantedAuthority[roles.length];
        for (int i = 0; i < roles.length; i++) {
            authorities[i] = new GrantedAuthorityImpl(roles[i]);
        }/*from   w  w  w .j  a  va2  s . c o m*/
        authToken = new UsernamePasswordAuthenticationToken(username, password,
                CollectionUtils.arrayToList(authorities));
    } else {
        authToken = new UsernamePasswordAuthenticationToken(username, password);
    }
    ctxImpl.setAuthentication(authToken);
    return ctxImpl;
}

From source file:de.fau.amos4.test.integration.helper.security.WithMockCustomUserSecurityContextFactory.java

@Override
public SecurityContext createSecurityContext(WithMockCustomUser customUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    // A empty user that is NOT in the database.
    Client mockClient = createClient(customUser);

    CurrentClient principal = new CurrentClient(mockClient);
    Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
            principal.getAuthorities());
    context.setAuthentication(auth);// www .j  a  v a 2s.  co  m
    return context;
}

From source file:org.cloudfoundry.identity.uaa.security.DefaultSecurityContextAccessorTests.java

@Test
public void adminUserIsAdmin() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken("user", "password", UaaAuthority.ADMIN_AUTHORITIES));

    assertTrue(new DefaultSecurityContextAccessor().isAdmin());
}

From source file:com.exp.tracker.services.impl.JpaAuthServiceTests.java

@Before
public void setup() {
    userDetailService = appContext.getBean(JdbcDaoImpl.class);
    UserDetails userDetails = userDetailService.loadUserByUsername("Admin");
    Authentication authToken = new UsernamePasswordAuthenticationToken(userDetails.getUsername(),
            userDetails.getPassword(), userDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(authToken);
    rCtx = new MockRequestContext();
    MockExternalContext ec = new MockExternalContext();
    ec.setCurrentUser("Admin");
    ((MockRequestContext) rCtx).setExternalContext(ec);
    // Add a user first, it will add a "ROLE_USER" by default.
    UserBean ub = new UserBean();
    ub.setEmailId("a@b.com");
    ub.setEnabled(true);/*  w  w w . j av  a2s . c o  m*/
    ub.setFirstName("Test");
    ub.setLastName("User");
    ub.setMiddleInit("1");
    ub.setPassword("password");
    ub.setUsername("testuser1");
    userBean = userService.addUser(ub, rCtx);
}