Search in sources :

Example 81 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class BindAuthenticatorTests method testAuthenticationWithWrongPasswordFails.

/*
	 * @Test public void messingWithEscapedChars() throws Exception {
	 * Hashtable<String,String> env = new Hashtable<String,String>();
	 * env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
	 * env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:22389/dc=springsource,dc=com");
	 * env.put(Context.SECURITY_AUTHENTICATION, "simple");
	 * env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=springsource,dc=com");
	 * env.put(Context.SECURITY_CREDENTIALS, "password");
	 *
	 * InitialDirContext idc = new InitialDirContext(env); SearchControls searchControls =
	 * new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
	 * DistinguishedName baseDn = new DistinguishedName("ou=\\\"quoted people\\\"");
	 * NamingEnumeration<SearchResult> matches = idc.search(baseDn, "(cn=*)", new Object[]
	 * {"quoteguy"}, searchControls);
	 *
	 * while(matches.hasMore()) { SearchResult match = matches.next(); DistinguishedName
	 * dn = new DistinguishedName(match.getName()); System.out.println("**** Match: " +
	 * match.getName() + " ***** " + dn);
	 *
	 * } }
	 */
@Test
public void testAuthenticationWithWrongPasswordFails() {
    this.authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
    try {
        this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "wrongpassword"));
        fail("Shouldn't be able to bind with wrong password");
    } catch (BadCredentialsException expected) {
    }
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 82 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class PasswordComparisonAuthenticatorTests method setUp.

// ~ Methods
// ========================================================================================================
@Before
public void setUp() throws Exception {
    authenticator = new PasswordComparisonAuthenticator(getContextSource());
    authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
    authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
    bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword");
    ben = new UsernamePasswordAuthenticationToken("ben", "benspassword");
}
Also used : PlaintextPasswordEncoder(org.springframework.security.authentication.encoding.PlaintextPasswordEncoder) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 83 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class FilterChainPerformanceTests method provideDataOnScalingWithNumberOfAuthoritiesUserHas.

/**
	 * Creates data from 1 to N_AUTHORITIES in steps of 10, performing N_INVOCATIONS for
	 * each
	 */
@Test
public void provideDataOnScalingWithNumberOfAuthoritiesUserHas() throws Exception {
    StopWatch sw = new StopWatch("Scaling with nAuthorities");
    for (int user = 0; user < N_AUTHORITIES / 10; user++) {
        int nAuthorities = user == 0 ? 1 : user * 10;
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("bob", "bobspassword", createRoles(nAuthorities)));
        session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());
        SecurityContextHolder.clearContext();
        sw.start(Integer.toString(nAuthorities) + " authorities");
        runWithStack(minimalStack);
        System.out.println(sw.shortSummary());
        sw.stop();
    }
    System.out.println(sw.prettyPrint());
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) StopWatch(org.springframework.util.StopWatch)

Example 84 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class PasswordComparisonAuthenticatorTests method testLdapPasswordCompareFailsWithWrongPassword.

@Test(expected = BadCredentialsException.class)
public void testLdapPasswordCompareFailsWithWrongPassword() {
    // Don't retrieve the password
    authenticator.setUserAttributes(new String[] { "uid", "cn", "sn" });
    authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "wrongpass"));
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 85 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class PasswordComparisonAuthenticatorTests method testUseOfDifferentPasswordAttributeSucceeds.

@Test
public void testUseOfDifferentPasswordAttributeSucceeds() {
    authenticator.setPasswordAttributeName("uid");
    authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "bob"));
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)309 Test (org.junit.Test)156 Authentication (org.springframework.security.core.Authentication)114 GrantedAuthority (org.springframework.security.core.GrantedAuthority)37 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)34 UserDetails (org.springframework.security.core.userdetails.UserDetails)33 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 SecurityContext (org.springframework.security.core.context.SecurityContext)21 AuthenticationException (org.springframework.security.core.AuthenticationException)20 User (org.springframework.security.core.userdetails.User)17 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)15 ArrayList (java.util.ArrayList)14 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)13 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)12 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)11 Before (org.junit.Before)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8