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.brekka.pegasus.core.security.UnlockAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(final String token, final UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    Object credentials = authentication.getCredentials();
    String password = credentials.toString();
    if (StringUtils.isBlank(password)) {
        throw new BadCredentialsException("A code is required");
    }/*from   w w  w.  j  a v  a2s  .  c om*/

    AnonymousTransferUser anonymousTransferUser = new AnonymousTransferUser(token);
    SecurityContext context = SecurityContextHolder.getContext();

    // Temporarily bind the authentication user to the security context so that we can do the unlock
    // this is primarily for the EventService to capture the IP/remote user.
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(anonymousTransferUser,
            anonymousTransferUser);
    auth.setDetails(authentication.getDetails());
    try {
        context.setAuthentication(auth);
        anonymousService.unlock(token, password, true);
        context.setAuthentication(null);
        return anonymousTransferUser;
    } catch (PhalanxException e) {
        if (e.getErrorCode() == PhalanxErrorCode.CP302) {
            throw new BadCredentialsException("Code appears to be incorrect");
        }
        throw e;
    }
}

From source file:org.jasig.schedassist.web.security.RemoteUserAuthenticationProcessingFilterImpl.java

public Authentication attemptAuthentication(final HttpServletRequest request,
        final HttpServletResponse response) throws AuthenticationException {
    String username = request.getRemoteUser();
    String password = "";
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,
            password);//from w  w  w  .ja va 2s  . c o m

    authRequest.setDetails(authenticationDetailsSource.buildDetails((HttpServletRequest) request));

    return this.getAuthenticationManager().authenticate(authRequest);
}

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

protected Authentication getAuthentication(String username) {
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority("user"));
    User user = new User(username, "password", true, false, false, false, authorities);
    Authentication authentication = new UsernamePasswordAuthenticationToken(user, null);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return authentication;
}

From source file:cn.com.esrichina.gcloud.commons.web.resources.SystemResource.java

private Authentication getAuthentication(String fullUsername, String password) throws GeneralException {
    if (!fullUsername.contains("@") || fullUsername.startsWith("admin@")) {
        return new UsernamePasswordAuthenticationToken(fullUsername, MD5.MD5Encode(password));
    } else {//  w  w w  .ja va  2  s.  c o m
        String username = fullUsername.split("@")[0];
        String accountShortName = fullUsername.split("@")[1];

        Account account = null;//accountService.getAccountByShortName(accountShortName);
        if (account == null) {
            throw new GeneralException(Messages.getMessage("account_not_exist"));
        }
        String securityConfig = account.getSecurityConfig();
        if (StringUtils.isBlank(securityConfig)) {
            return new UsernamePasswordAuthenticationToken(fullUsername, MD5.MD5Encode(password));
        } else {
            JSONObject jsonObject = JSONObject.fromObject(securityConfig);
            if (jsonObject.containsKey("type") && jsonObject.getString("type").equals("LDAP")) {
                JSONObject propertiesObj = jsonObject.getJSONObject("properties");
                LDAPConfig ldapConfig = new LDAPConfig();
                // ???
                ldapConfig.setIp(propertiesObj.getString("ip"));
                ldapConfig.setPort(propertiesObj.getInt("port"));
                ldapConfig.setUser(propertiesObj.getString("user"));
                ldapConfig.setUserPassword(propertiesObj.getString("userPassword"));
                ldapConfig.setIsPasswordEncrypted(propertiesObj.getString("isPasswordEncrypted"));
                ldapConfig.setUserBaseDN(propertiesObj.getString("userBaseDN"));
                ldapConfig.setUserEmailAttribute(propertiesObj.getString("userEmailAttribute"));
                ldapConfig.setUserFullnameAttribute(propertiesObj.getString("userFullnameAttribute"));
                ldapConfig.setUsernameAttribute(propertiesObj.getString("usernameAttribute"));
                ldapConfig.setUserSearchAttribute(propertiesObj.getString("userSearchAttribute"));
                ldapConfig.setCaseSensitive(propertiesObj.getString("caseSensitive"));
                return new LDAPUsernamePasswordAuthenticationToken(ldapConfig, account.getId(), username,
                        password);
            } else {
                throw new GeneralException(Messages.getMessage("account_unsupport_securitytype"));
            }
        }
    }
}

From source file:com.sun.identity.provider.springsecurity.OpenSSOProcessingFilter.java

public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException {

    SSOToken token = obtainSSOToken(request);
    String username = obtainUsername(token);
    if (debug.messageEnabled())
        debug.message("username: " + (username == null ? "is null" : username));

    if (username == null) {
        throw new BadCredentialsException("User not logged in via Portal! SSO user cannot be validated!");
    }//  w  w  w .  j  a v  a2s  . c  om

    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, token);

    // Place the last username attempted into HttpSession for views
    request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username);

    setDetails(request, authRequest);

    return this.getAuthenticationManager().authenticate(authRequest);
}

From source file:org.createnet.raptor.auth.service.jwt.JsonUsernamePasswordFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException {

    if (!request.getMethod().equals("POST")) {
        throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
    }/*w w  w .  j a va2s  .  com*/

    if (!request.getContentType().equals(MediaType.APPLICATION_JSON)) {
        throw new AuthenticationServiceException("Only Content-Type " + MediaType.APPLICATION_JSON
                + " is supported. Provided is " + request.getContentType());
    }

    LoginForm loginForm;
    try {
        InputStream body = request.getInputStream();
        loginForm = jacksonObjectMapper.readValue(body, LoginForm.class);
    } catch (IOException ex) {
        throw new AuthenticationServiceException("Error reading body", ex);
    }

    if (loginForm.username == null) {
        loginForm.username = "";
    }

    if (loginForm.password == null) {
        loginForm.password = "";
    }

    loginForm.username = loginForm.username.trim();

    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
            loginForm.username, loginForm.password);
    setDetails(request, authRequest);

    return this.getAuthenticationManager().authenticate(authRequest);
}

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

@Test
public void testNotProcessingWrongType() {
    Authentication authentication = manager.authenticate(new UsernamePasswordAuthenticationToken("foo", "bar"));
    assertNull(authentication);/*from w ww . j  ava2 s .  co  m*/
}

From source file:de.itsvs.cwtrpc.security.UsernamePasswordRpcAuthenticationFilter.java

@Override
public Authentication createAuthenticationToken(HttpServletRequest servletRequest, RPCRequest rpcRequest)
        throws AuthenticationException, IOException, ServletException {
    final UsernamePasswordAuthenticationToken authentication;
    String username;//w ww . j a va 2s .c o  m
    String password;

    username = getUsernameResolver().resolveValue(rpcRequest.getParameters());
    if (username == null) {
        username = "";
    }
    password = getPasswordResolver().resolveValue(rpcRequest.getParameters());
    if (password == null) {
        password = "";
    }

    authentication = new UsernamePasswordAuthenticationToken(username, password);
    updateDetails(servletRequest, authentication);

    return authentication;
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.security.SpringAuthenticatedWebSession.java

@Override
public boolean authenticate(String username, String password) {
    boolean authenticated = false;
    try {/* ww w.jav a2  s .c om*/
        Authentication authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(username, password));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        authenticated = authentication.isAuthenticated();
    } catch (AuthenticationException e) {
        log.warn(format("User '%s' failed to login. Reason: %s", username, e.getMessage()));
        authenticated = false;
    }
    return authenticated;
}

From source file:com.greenbird.mule.http.log.converter.UserNameConverterTest.java

private MuleSession securedSession(Object principal) {
    MuleSession session = new DefaultMuleSession();
    session.setSecurityContext(new DefaultSecurityContext(
            new SpringAuthenticationAdapter(new UsernamePasswordAuthenticationToken(principal, null))));
    return session;
}