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:org.chance.SampleSecureApplicationTests.java

@Before
public void init() {
    AuthenticationManager authenticationManager = this.context.getBean(AuthenticationManager.class);
    this.authentication = authenticationManager
            .authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
}

From source file:org.messic.server.facade.security.CustomUsernamePasswordAuthenticationFilter.java

/**
* 
*///from  w w w  .java 2s  .c o m
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        Authentication authResult) throws IOException, ServletException {

    if (logger.isDebugEnabled()) {
        logger.debug("Authentication success. Updating SecurityContextHolder to contain: " + authResult);
    }

    SecurityContextHolder.getContext().setAuthentication(authResult);

    getRememberMeServices().loginSuccess(request, response, authResult);

    // Fire event
    if (this.eventPublisher != null) {
        eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
    }

    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
            obtainUsername(request), obtainPassword(request));

    String token = AuthenticationSessionManager.successfulAuthentication(authentication);

    request.setAttribute("messic_token", token);

    getSuccessHandler().onAuthenticationSuccess(request, response, authResult);

}

From source file:com.jevontech.wabl.controllers.AuthenticationController.java

@CrossOrigin
//@RequestMapping("/auth")
@RequestMapping(value = "/auth", method = RequestMethod.POST)
public ResponseEntity<?> authenticationRequest(@RequestBody AuthenticationRequest authenticationRequest,
        Device device) throws AuthenticationException {

    // Perform the authentication
    Authentication authentication = this.authenticationManager
            .authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(),
                    authenticationRequest.getPassword()));
    SecurityContextHolder.getContext().setAuthentication(authentication);

    // Reload password post-authentication so we can generate token
    UserDetails userDetails = this.userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
    String token = this.tokenUtils.generateToken(userDetails, device);

    // Return the token
    return ResponseEntity.ok(new AuthenticationResponse(token));
}

From source file:nz.co.senanque.vaadinsupport.viewmanager.SpringLoginListener.java

private void login(String username, String password) {
    Set<String> permissionsList = new HashSet<String>();
    try {/*from w  ww  .  ja  v  a 2s  . c  o m*/
        Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
        authentication = getAuthenticationManager().authenticate(authentication);
        for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
            permissionsList.add(grantedAuthority.getAuthority());
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    getPermissionManager().setPermissionsList(permissionsList);
    getPermissionManager().setCurrentUser(username);
}

From source file:com.github.carlomicieli.nerdmovies.test.AbstractSpringControllerTests.java

protected void login(String emailAddress, String password) {
    Authentication authentication = new UsernamePasswordAuthenticationToken(
            new MailUser(emailAddress, password), null);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:org.joyrest.oauth2.BasicAuthenticator.java

public Authentication authenticate(Request<?> request) {
    String header = request.getHeader("Authorization")
            .orElseThrow(() -> new BadCredentialsException("There is no Authorization header"));

    if (isNull(header) || !header.startsWith("Basic ")) {
        throw new BadCredentialsException("Failed to decode basic authentication token");
    }/*from   w  w  w .j a  va  2 s.  c o m*/

    try {
        String[] tokens = extractAndDecodeHeader(header);
        String username = tokens[0];

        logger.debug(() -> "Basic Authentication Authorization header found for user '" + username + "'");

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,
                tokens[1]);

        Authentication authentication = authenticationManager.authenticate(authRequest);
        logger.debug(() -> "Authentication success: " + authentication);
        return authentication;
    } catch (IOException ex) {
        throw new BadCredentialsException("Failed to decode basic authentication token");
    }
}

From source file:fr.xebia.springframework.security.core.providers.ExtendedDaoAuthenticationProviderTest.java

private void testAdditionalchecks(String allowedRemoteAddresses, String remoteAddr) {
    ExtendedDaoAuthenticationProvider daoAuthenticationProvider = new ExtendedDaoAuthenticationProvider();

    Collection<GrantedAuthority> grantedAuthorities = Collections.emptyList();

    ExtendedUser extendedUser = new ExtendedUser("test-user", "test-password", true, true, true, true,
            grantedAuthorities);//from   w w w .j av  a2 s. c o  m

    extendedUser.setAllowedRemoteAddresses(allowedRemoteAddresses);

    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("test-user",
            "test-password");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr(remoteAddr);
    authentication.setDetails(new WebAuthenticationDetails(request));

    daoAuthenticationProvider.additionalAuthenticationChecks(extendedUser, authentication);
}

From source file:be.bittich.quote.controller.impl.AuthControllerImpl.java

@Override
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)/*w w  w.  ja  v  a2  s.  com*/
public SecurityToken authenticate(@Context HttpServletRequest request, @RequestBody @Valid UserVO userVO) {
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
            userVO.getUsername(), userVO.getPassword());

    Authentication authentication = authenticationManager.authenticate(authenticationToken);

    SecurityContextHolder.getContext().setAuthentication(authentication);

    UserDetails userDetails = this.userService.loadUserByUsername(userVO.getUsername());

    SecurityToken createToken = tokenService.createToken(userDetails, request.getRemoteAddr());
    return createToken;
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaAuthenticationManagerTests.java

@Test
public void testAuthenticate() throws Exception {
    responseHeaders.setLocation(new URI("https://uaa.cloudfoundry.com/"));
    Map<String, String> response = new HashMap<String, String>();
    response.put("username", "marissa");
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> expectedResponse = new ResponseEntity<Map>(response, responseHeaders, HttpStatus.OK);
    when(restTemplate.exchange(endsWith("/authenticate"), eq(HttpMethod.POST), any(HttpEntity.class),
            eq(Map.class))).thenReturn(expectedResponse);
    Authentication result = authenticationManager
            .authenticate(new UsernamePasswordAuthenticationToken("marissa", "foo"));
    assertEquals("marissa", result.getName());
    assertTrue(result.isAuthenticated());
}