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.evidence.data.DbUnitTest.java

@Before
public void init() throws DatabaseUnitException, SQLException, MalformedURLException {
    Authentication authentication = new UsernamePasswordAuthenticationToken("admin@evidence.com", "password");
    Authentication authenticate = authenticationManager.authenticate(authentication);
    SecurityContextHolder.getContext().setAuthentication(authenticate);
    // insert data into database
    DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
}

From source file:ru.codemine.ccms.router.api.ApiSecurityRouter.java

@RequestMapping(value = "/api/login", method = RequestMethod.POST)
public ResponseEntity<?> authRequest(HttpServletRequest req) {
    String reqUsername = req.getHeader("username");
    String reqPass = req.getHeader("password");

    String token = null;/*from  ww w.j  av  a  2  s.  c om*/
    Map<String, String> headers = new HashMap();

    try {
        Authentication authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(reqUsername, reqPass));

        SecurityContextHolder.getContext().setAuthentication(authentication);

        Employee employee = employeeService.getByUsername(reqUsername);
        token = apiTokenUtils.generateToken(employee);

        headers.put("X-Auth-Token", token);
    } catch (BadCredentialsException e) {
        log.warn("    , ?: "
                + reqUsername);
    }

    return token == null ? new ResponseEntity<>(HttpStatus.FORBIDDEN)
            : new ResponseEntity<>(headers, HttpStatus.OK);
}

From source file:com.epam.ta.reportportal.auth.event.AuthenticationSuccessEventTest.java

@Override
protected Authentication authentication() {
    return new UsernamePasswordAuthenticationToken(AuthConstants.TEST_USER, AuthConstants.USER_PASSWORD);
}

From source file:fr.esiea.esieaddress.controllers.login.authentication.AuthenticationCtrl.java

@RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json")
@ResponseBody/*w  w w  .j  a  v  a 2 s. c  o m*/
@ResponseStatus(HttpStatus.OK)
public void login(@RequestBody User user, HttpServletResponse response) throws ServiceException, DaoException {

    LOGGER.info("[Controller] Querying to log in User \"" + user.toString() + "\"");

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getMail(),
            user.getPassword());
    authService.login(token);

}

From source file:example.springdata.jpa.security.SecurityIntegrationTests.java

@Before
public void setup() {

    tom = userRepository.save(new User("thomas", "darimont", "tdarimont@example.org"));
    ollie = userRepository.save(new User("oliver", "gierke", "ogierke@example.org"));
    admin = userRepository.save(new User("admin", "admin", "admin@example.org"));

    object1 = businessObjectRepository.save(new BusinessObject("object1", ollie));
    object2 = businessObjectRepository.save(new BusinessObject("object2", ollie));
    object3 = businessObjectRepository.save(new BusinessObject("object3", tom));

    olliAuth = new UsernamePasswordAuthenticationToken(ollie, "x");
    tomAuth = new UsernamePasswordAuthenticationToken(tom, "x");
    adminAuth = new UsernamePasswordAuthenticationToken(admin, "x",
            singleton(new SimpleGrantedAuthority("ROLE_ADMIN")));
}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.ScopeAuthenticationManagerTest.java

public void testPasswordAuthenticateSucceed() throws Exception {
    UsernamePasswordAuthenticationToken userAuth = new UsernamePasswordAuthenticationToken("username",
            "password");
    userAuth.setAuthenticated(true);/*from   w  ww  .j ava 2 s.co  m*/
    OAuth2Authentication auth = new OAuth2Authentication(request, userAuth);
    Authentication authentication = authenticationManager.authenticate(auth);
    assertTrue(authentication.isAuthenticated());
}

From source file:org.unidle.repository.AuditorAwareImplTest.java

@Test
public void testGetCurrentAuditor() throws Exception {
    final Authentication authentication = new UsernamePasswordAuthenticationToken(user.getUuid(), null);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    final Object result = subject.getCurrentAuditor();

    assertThat(result).isEqualTo(user);// ww w.j  ava  2  s  .  c o m
}

From source file:com.boxedfolder.carrot.web.client.security.UserXAuthTokenResource.java

@RequestMapping(value = "/client/authenticate", method = { RequestMethod.POST })
public UserTransfer authorize(@RequestParam String username, @RequestParam String password) {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    Authentication authentication = authenticationManager.authenticate(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    UserDetails details = userDetailsService.loadUserByUsername(username);
    Map<String, Boolean> roles = new HashMap<>();
    for (GrantedAuthority authority : details.getAuthorities()) {
        roles.put(authority.toString(), Boolean.TRUE);
    }/*  w w w  .jav a 2  s.co  m*/

    return new UserTransfer(details.getUsername(), roles, tokenUtils.createToken(details));
}

From source file:sk.lazyman.gizmo.security.GizmoAuthWebSession.java

@Override
public boolean authenticate(String username, String password) {
    LOGGER.debug("Authenticating '{}' {} password in web session.",
            new Object[] { username, (StringUtils.isEmpty(password) ? "without" : "with") });

    boolean authenticated;
    try {/*from w ww  .j av a2s .co  m*/
        Authentication authentication = authenticationProvider
                .authenticate(new UsernamePasswordAuthenticationToken(username, password));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        authenticated = authentication.isAuthenticated();
    } catch (AuthenticationException ex) {
        LOGGER.error("Couldn't authenticate user, reason: {}", ex.getMessage());
        LOGGER.debug("Couldn't authenticate user.", ex);
        authenticated = false;

        String msg = new StringResourceModel(ex.getMessage(), null, ex.getMessage()).getString();
        error(msg);
    }

    return authenticated;
}

From source file:com.ushahidi.swiftriver.core.api.controller.BucketsControllerTest.java

@Before
public void before() {
    authentication = new UsernamePasswordAuthenticationToken("user1", "password");
    SecurityContextHolder.getContext().setAuthentication(authentication);
}