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.application.model.dao.AuthenticationService.java

public void handleAuthentication(String login, String password, HttpServletRequest httpRequest) {

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(login, password);

    token.setDetails(new WebAuthenticationDetails(httpRequest));

    ServletContext servletContext = httpRequest.getSession().getServletContext();

    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

    AuthenticationManager authManager = wac.getBean(AuthenticationManager.class);

    Authentication authentication = authManager.authenticate(token);

    SecurityContextHolder.getContext().setAuthentication(authentication);

}

From source file:com.morevaadin.vaadin7.springsecurity.service.AuthenticationService.java

public void handleAuthentication(String login, String password, HttpServletRequest httpRequest) {

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(login, password);

    token.setDetails(new WebAuthenticationDetails(httpRequest));

    ServletContext servletContext = httpRequest.getSession().getServletContext();

    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

    AuthenticationManager authManager = wac.getBean(AuthenticationManager.class);

    Authentication authentication = authManager.authenticate(token);

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

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

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

    String organisation = identityService.getOrganisation(s);

    String expectedOrganisation = "moorfields";
    assertEquals(expectedOrganisation, organisation);
}

From source file:org.unidle.social.SignInAdapterImpl.java

@Override
public String signIn(final String userId, final Connection<?> connection, final NativeWebRequest request) {

    final Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null);

    SecurityContextHolder.getContext().setAuthentication(authentication);

    final Cookie cookie = new Cookie(LAST_LOGIN_SOURCE.getName(), connection.createData().getProviderId());
    cookie.setMaxAge(LAST_LOGIN_SOURCE.getMaxAgeAs(SECONDS));
    cookie.setPath("/");

    request.getNativeResponse(HttpServletResponse.class).addCookie(cookie);

    return null;/*from  www .  j  a  v  a2  s .  c o m*/
}

From source file:com.pamarin.income.security.DefaultBasicAuthenImpl.java

/**
 * @param username/*from  w  ww .j a  v a2  s .  co  m*/
 * @param password
 */
@Override
public void login(String username, String password) throws UsernameNotFoundException {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    //check authen from user details service
    UserDetails userDetails = detailsService.loadUserByUsername(username);
    Authentication authentication = manager
            .authenticate(new UsernamePasswordAuthenticationToken(userDetails, password));
    //keep authentication to security context 
    securityContext.setAuthentication(authentication);
}

From source file:de.qucosa.spring.RoleParameterAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String role = request.getParameter("role");
    if (role != null) {
        SecurityContext sctx = SecurityContextHolder.getContext();
        if (sctx.getAuthentication() == null) {
            sctx.setAuthentication(new UsernamePasswordAuthenticationToken(role, role));
        }/* ww w . j  a va2  s  .co m*/
    }
    chain.doFilter(request, response);
}

From source file:org.shredzone.cilla.ws.client.RemoteLoginServiceImpl.java

@Override
public void login(String user, String password) {
    RemoteUserDetails userDetails = new RemoteUserDetails();
    userDetails.setUsername(user);/*from   ww w  .j  a va 2  s. c  om*/
    userDetails.setPassword(password);

    Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, password);
    SecurityContextHolder.getContext().setAuthentication(auth);

    auth = authenticationManager.authenticate(auth);
    SecurityContextHolder.getContext().setAuthentication(auth);
}

From source file:com.gsr.myschool.server.security.AdminAuthenticationServiceImpl.java

@Override
public Boolean authenticate(String username, String password) {
    Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
    try {/*from   ww w  .ja v  a 2  s.  c o m*/
        Authentication authenticated = authenticationManager.authenticate(authentication);
        return authenticated.isAuthenticated();
    } catch (Exception e) {
        return false;
    }
}

From source file:org.springbyexample.contact.test.AbstractTransactionalProfileTest.java

/**
 * Set the default user on the security context.
 */// w  w w. j av  a 2  s  .  co  m
@Override
protected void doInit() {
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken(DEFAULT_SECURITY_USER, DEFAULT_SECURITY_USER_PASSWORD));
}

From source file:com.epam.cme.storefront.security.impl.DefaultAutoLoginStrategy.java

@Override
public void login(final String username, final String password, final HttpServletRequest request,
        final HttpServletResponse response) {
    final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username,
            password);// w ww  . j  av a 2 s  . com
    token.setDetails(new WebAuthenticationDetails(request));
    try {
        final Authentication authentication = getAuthenticationManager().authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        getCustomerFacade().loginSuccess();
        getGuidCookieStrategy().setCookie(request, response);
    } catch (final Exception e) {
        SecurityContextHolder.getContext().setAuthentication(null);
        LOG.error("Failure during autoLogin", e);
    }
}