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:cz.cvut.fel.wpa.tracker.pres.bb.LoginBean.java

public String login() {
    Authentication request = new UsernamePasswordAuthenticationToken(username, password);
    try {// w ww  . ja v a2s  .c  o  m
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
    } catch (org.springframework.security.core.AuthenticationException e) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                "Invalid credentials!", "The username/password combination is not valid."));
        return "login";
    }
    FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_WARN, "Login ok", "login ok."));
    return "issues?faces-redirect=true";
}

From source file:com.vaadinspring.presenter.LoginPresenterImpl.java

public boolean doLogin(String username, String password) {
    auth = authProvider.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    if (auth == null)
        return false;
    else//from  w ww .  j  a v  a2 s.c  om
        return true;
}

From source file:de.thm.arsnova.services.StubUserService.java

public void setUserAuthenticated(boolean isAuthenticated, String username) {
    if (isAuthenticated) {
        stubUser = new User(new UsernamePasswordAuthenticationToken(username, "testpassword"));
        return;//from   w w  w. j av  a 2 s.c o  m
    }
    stubUser = null;
}

From source file:org.elasticstore.server.service.impl.ElasticSearchAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    final String username = authentication.getName();
    return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
            authentication.getCredentials());
}

From source file:de.thm.arsnova.services.StubUserService.java

public void useAnonymousUser() {
    stubUser = new User(new UsernamePasswordAuthenticationToken("anonymous", ""));
}

From source file:com.companyname.LoginTest.java

@Test
public void authenticateUserTest() {
    logger.info("running test to authenticate a user ");
    try {/*from   ww  w.j a  v a 2 s .  co m*/
        Authentication request = new UsernamePasswordAuthenticationToken("toy", "toy");
        Authentication result = provider.authenticate(request);

        //SecurityContextHolder.getContext().setAuthentication(result);
        //SecurityContextHolder.getContext().getAuthentication()

        logger.info("Successfully authenticated. Security context contains: " + result);

        assertThat(result, IsNull.notNullValue());

    } catch (AuthenticationException e) {
        System.out.println("Authentication Test failed: " + e.getMessage());
    }
}

From source file:org.camelcookbook.security.springsecurity.SecuritySubjectLoader.java

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();//from  ww  w.  j a v  a  2 s  .  c om
    String username = in.getHeader("username", String.class);
    String password = in.getHeader("password", String.class);

    Authentication authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    Subject subject = new Subject();
    subject.getPrincipals().add(authenticationToken);
    in.setHeader(Exchange.AUTHENTICATION, subject);
}

From source file:org.cloudfoundry.identity.uaa.oauth.token.TokenKeyEndpointTests.java

@Test
public void sharedSecretIsReturnedFromTokenKeyEndpoint() throws Exception {
    signerProvider.setVerifierKey("someKey");
    assertEquals("{alg=HMACSHA256, value=someKey}",
            tokenEnhancer.getKey(new UsernamePasswordAuthenticationToken("foo", "bar")).toString());
}

From source file:org.camelcookbook.security.springsecurity.SecurityContextLoader.java

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();//w  ww  .  j  av a 2  s  . c o  m
    String username = in.getHeader("username", String.class);
    String password = in.getHeader("password", String.class);

    Authentication authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}

From source file:fi.helsinki.opintoni.security.SecurityUtilsTest.java

@Test
public void testGetCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
    SecurityContextHolder.setContext(securityContext);
    String login = securityUtils.getCurrentLogin();
    assertThat(login).isEqualTo("admin");
}