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) 

Source Link

Document

This constructor can be safely used by any code that wishes to create a UsernamePasswordAuthenticationToken, as the #isAuthenticated() will return false.

Usage

From source file:com.mtt.myapp.user.service.UserContextTest.java

@Test
public void testGetUser() {
    UserContext userCtx = new UserContext();

    //in super.beforeSetSecurity(), there is an admin user is set, but the auth is invalid
    try {/*from  ww  w .  j av a  2  s . co  m*/
        userCtx.getCurrentUser();
        assertTrue(false);
    } catch (AuthenticationCredentialsNotFoundException e) {
        assertTrue(true);
    }

    UserDetails user = securityUserDetailService.loadUserByUsername(getTestUser().getUserId());

    Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication();

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "123");
    SecurityContextHolder.getContext().setAuthentication(token);
    userCtx.getCurrentUser();
    assertTrue(true);

    SecurityContextHolder.getContext().setAuthentication(oriAuth);
}

From source file:com.jeanchampemont.notedown.security.AuthenticationService.java

public void newAuthentication(String email, String password) {
    Authentication request = new UsernamePasswordAuthenticationToken(email, password);
    Authentication result = authenticationManager.authenticate(request);
    SecurityContextHolder.getContext().setAuthentication(result);
}

From source file:info.gewton.slsecurity.test.Test.java

/**
 * Efetua autenticao, criando um contexto do Spring Security
 * @param login usurio//from  w  ww. ja v  a 2  s .c o  m
 * @param password senha
 */
protected void setSecurityContext(String login, String password) {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(login, password));
}

From source file:org.ngrinder.user.service.UserContextTest.java

@Test
public void testGetUser() {
    UserContext userCtx = new UserContext();

    //in super.beforeSetSecurity(), there is an admin user is set, but the auth is invalid
    try {//from  w w  w.j a  v a2 s  .  co m
        userCtx.getCurrentUser();
        assertTrue(false);
    } catch (AuthenticationCredentialsNotFoundException e) {
        assertTrue(true);
    }

    UserDetails user = userDetailService.loadUserByUsername(getTestUser().getUserId());

    Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication();

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "123");
    SecurityContextHolder.getContext().setAuthentication(token);
    userCtx.getCurrentUser();
    assertTrue(true);

    SecurityContextHolder.getContext().setAuthentication(oriAuth);
}

From source file:de.uni_koeln.spinfo.maalr.login.LoginManager.java

public Authentication login(String name, String password) {
    logout();/*from w ww . ja v  a  2  s  .  c o  m*/
    try {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(name, password);
        Authentication authenticate = provider.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authenticate);
        logger.info("Successfully logged in user " + name);
        return authenticate;
    } catch (AuthenticationException e) {
        logger.warn("Failed to log in user " + name + ", reason: " + e);
        logger.warn("Details", e);
        return null;
    }
}

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

@Test
public void testAuthenticateUser() throws Exception {
    crowdAuthenticationProviderMock.authenticate(new UsernamePasswordAuthenticationToken("user", "user"));
}

From source file:org.shredzone.cilla.ws.cxf.CillaUsernameTokenValidator.java

@Override
protected void verifyPlaintextPassword(UsernameToken usernameToken, RequestData data)
        throws WSSecurityException {
    try {//from   w ww . j  a  v  a2 s.  c o m
        Authentication auth = new UsernamePasswordAuthenticationToken(usernameToken.getName(),
                usernameToken.getPassword());
        auth = authenticationManager.authenticate(auth);
        SecurityContextHolder.getContext().setAuthentication(auth);
        log.debug("Successfully authenticated user {}", usernameToken.getName());
    } catch (AuthenticationException ex) {
        log.error("Unable to authenticate user {}: {}", usernameToken.getName(), ex.getMessage());
        throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);
    }
}

From source file:example.springdata.mongodb.security.PersonRepositoryIntegrationTest.java

@Test
public void nonAdminCallingShouldReturnOnlyItSelfAsPerson() throws Exception {

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(dave, "x"));

    List<Person> persons = repository.findAllForCurrentUserById();

    assertThat(persons, hasSize(1));//from  w w w  .  j av  a2 s  .  com
    assertThat(persons, contains(dave));
}

From source file:io.dacopancm.jfee.managedController.LoginBean.java

public String login() {
    try {//  w  ww. j ava2 s  . c o  m
        Authentication request = new UsernamePasswordAuthenticationToken(this.getUserName(),
                this.getPassword());
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);

    } catch (BadCredentialsException bc) {
        log.info("jfee: " + bc.getMessage());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "Login Incorrecto", "Usuario o contrasea incorrecto"));
        usuarioService.failLoginUser(this.getUserName());
        return null;
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "Login Incorrecto", "Cuenta inhabilitada o no confirmada."));
        return null;
    }

    User userDetails = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido", "Hola, " + userDetails.getUsername()));
    return "correct";
}

From source file:org.jtalks.common.security.acl.JtalksSidFactoryTest.java

@Test
public void testCreatePrincipal_anonymousUser() throws Exception {
    String username = "anonymousUser";
    Sid anonymousUser = sidFactory.createPrincipal(new UsernamePasswordAuthenticationToken(username, ""));
    assertSame(anonymousUser, UserSid.createAnonymous());
}