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.healthcit.cacure.businessdelegates.export.DataImporterTest.java

protected void login(String username, String password) {
    SecurityContextHolder.getContext()/* ww  w  .j  a v  a 2s .  c o  m*/
            .setAuthentication(new UsernamePasswordAuthenticationToken(username, password));

    //       logger.debug("User:" + username + " logged in");
}

From source file:org.springframework.security.jackson2.UsernamePasswordAuthenticationTokenMixinTest.java

@Test
public void serializeUnauthenticatedUsernamePasswordAuthenticationTokenMixinTest()
        throws JsonProcessingException, JSONException {
    String expectedJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\","
            + " \"principal\": \"user1\", \"credentials\": \"password\", \"authenticated\": false, \"details\": null, "
            + "\"authorities\": [\"java.util.ArrayList\", []], \"name\": \"user1\"}";
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user1", "password");
    String serializedJson = buildObjectMapper().writeValueAsString(token);
    JSONAssert.assertEquals(expectedJson, serializedJson, true);
}

From source file:net.cristcost.study.services.ServiceTestUtil.java

private static SecurityContext authenticate(PrintWriter writer, HttpServletRequest request,
        AuthenticationManager authenticationManager) {

    SecurityContext initialContext = SecurityContextHolder.getContext();

    if (request.getParameter("user") != null) {

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                request.getParameter("user"), request.getParameter("pass"));
        try {/*from   ww w.jav  a2 s .c  o  m*/
            Authentication authentication = authenticationManager.authenticate(authRequest);
            SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            writer.println("Authenticating user: " + request.getParameter("user"));
        } catch (AuthenticationException e) {
            writer.println("! Error while Authenticating: " + e.getMessage());
        }
        writer.println();
    }

    return initialContext;
}

From source file:cherry.foundation.springmvc.OperationLogHandlerInterceptorTest.java

@Before
public void before() {
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();

    SecurityContext context = createEmptyContext();
    context.setAuthentication(new UsernamePasswordAuthenticationToken("user01", "password"));
    setContext(context);/*w w w.  ja v  a2  s  . co m*/
}

From source file:com.sentinel.config.StatelessLoginFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {
    LOG.trace("Method: attemptAuthentication called.");
    final UserDto user = new ObjectMapper().readValue(request.getInputStream(), UserDto.class);
    final UsernamePasswordAuthenticationToken loginToken = new UsernamePasswordAuthenticationToken(
            user.getEmail(), user.getPassword());
    return getAuthenticationManager().authenticate(loginToken);
}

From source file:org.openwms.client.fx.core.controller.LoginController.java

/**
 * FIXME [scherrer] Comment this//from  ww w .  ja v a 2 s . c o m
 * 
 */
@FXML
public void login() {
    Authentication authToken = new UsernamePasswordAuthenticationToken(username.getText(), password.getText());
    try {
        authToken = authenticationManager.authenticate(authToken);
        SecurityContextHolder.getContext().setAuthentication(authToken);
    } catch (AuthenticationException e) {
        header.setText("Login failure, please try again:");
        header.setTextFill(Color.DARKRED);
        return;
    }
    dialog.close();
    screens.showScreen(screens.customerDataScreen());
}

From source file:ru.org.linux.auth.LoginController.java

@RequestMapping(value = "/login_process", method = RequestMethod.POST)
public ModelAndView loginProcess(@RequestParam("nick") final String username,
        @RequestParam("passwd") final String password, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    try {//from  w  w  w . j av  a  2  s .  c  o  m
        UserDetailsImpl details = (UserDetailsImpl) userDetailsService.loadUserByUsername(username);
        token.setDetails(details);
        Authentication auth = authenticationManager.authenticate(token);
        UserDetailsImpl userDetails = (UserDetailsImpl) auth.getDetails();
        if (!userDetails.getUser().isActivated()) {
            throw new AccessViolationException("User not activated");
        }
        SecurityContextHolder.getContext().setAuthentication(auth);
        rememberMeServices.loginSuccess(request, response, auth);
        AuthUtil.updateLastLogin(auth, userDao);
    } catch (Exception e) {
        return new ModelAndView(new RedirectView("/login.jsp?error=true"));
    }
    return new ModelAndView(new RedirectView("/"));
}

From source file:com.hillert.botanic.controller.AuthenticationController.java

@RequestMapping(value = "/authenticate", method = { RequestMethod.POST })
public AuthenticationToken authorize(@RequestBody AuthenticationRequest authenticationRequest,
        HttpServletRequest request) {//from ww  w .j av  a2  s .  com

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
            authenticationRequest.getUsername(), authenticationRequest.getPassword());
    Authentication authentication = this.authenticationManager.authenticate(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    HttpSession session = request.getSession(true);
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());

    UserDetails details = this.userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

    final Map<String, Boolean> roles = new HashMap<String, Boolean>();

    for (GrantedAuthority authority : details.getAuthorities()) {
        roles.put(authority.toString(), Boolean.TRUE);
    }

    return new AuthenticationToken(details.getUsername(), roles, session.getId());
}

From source file:it.geosolutions.geostore.rest.security.UserLdapAuthenticationProviderTest.java

@Test
public void testNullPassword() throws NotFoundServiceEx {
    provider.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
    User user = userService.get("user");
    assertNotNull(user);/*from  www. j  a v  a  2  s.  c  om*/
    assertNull(user.getPassword());
}

From source file:uk.org.openeyes.oink.security.TestSimpleIdentityService.java

@Test
public void testGetUserIdForValidSubject() {
    SimpleIdentityService identityService = new SimpleIdentityService();
    Subject s = new Subject();
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("bob@moorfields",
            "password");
    s.getPrincipals().add(token);/*from w w  w . j  a  v  a  2  s.c  o  m*/

    String user = identityService.getUserId(s);

    String expectedUser = "bob";
    assertEquals(expectedUser, user);
}