Search in sources :

Example 1 with User

use of org.springframework.security.core.userdetails.User in project spring-security by spring-projects.

the class SecurityMockMvcRequestPostProcessorsDigestTests method setup.

@Before
public void setup() {
    this.password = "password";
    request = new MockHttpServletRequest();
    entryPoint = new DigestAuthenticationEntryPoint();
    entryPoint.setKey("key");
    entryPoint.setRealmName("Spring Security");
    filter = new DigestAuthenticationFilter();
    filter.setUserDetailsService(new UserDetailsService() {

        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER"));
        }
    });
    filter.setAuthenticationEntryPoint(entryPoint);
    filter.afterPropertiesSet();
}
Also used : DigestAuthenticationEntryPoint(org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DigestAuthenticationFilter(org.springframework.security.web.authentication.www.DigestAuthenticationFilter) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Before(org.junit.Before)

Example 2 with User

use of org.springframework.security.core.userdetails.User in project spring-security by spring-projects.

the class AuthenticationConfigurationGh3935Tests method delegateUsesExisitingAuthentication.

@Test
public void delegateUsesExisitingAuthentication() {
    String username = "user";
    String password = "password";
    User user = new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER"));
    when(this.uds.loadUserByUsername(username)).thenReturn(user);
    AuthenticationManager authenticationManager = this.adapter.authenticationManager;
    assertThat(authenticationManager).isNotNull();
    Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    verify(this.uds).loadUserByUsername(username);
    assertThat(auth.getPrincipal()).isEqualTo(user);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 3 with User

use of org.springframework.security.core.userdetails.User in project spring-security by spring-projects.

the class AbstractPreAuthenticatedProcessingFilterTests method requiresAuthenticationFalsePrincipalUser.

@Test
public void requiresAuthenticationFalsePrincipalUser() throws Exception {
    User currentPrincipal = new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
    UsernamePasswordAuthenticationToken currentAuthentication = new UsernamePasswordAuthenticationToken(currentPrincipal, currentPrincipal.getPassword(), currentPrincipal.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(currentAuthentication);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter();
    filter.setCheckForPrincipalChanges(true);
    filter.principal = new User(currentPrincipal.getUsername(), currentPrincipal.getPassword(), AuthorityUtils.NO_AUTHORITIES);
    AuthenticationManager am = mock(AuthenticationManager.class);
    filter.setAuthenticationManager(am);
    filter.afterPropertiesSet();
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(am);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with User

use of org.springframework.security.core.userdetails.User in project spring-security by spring-projects.

the class AbstractRememberMeServicesTests method autoLoginShouldFailIfUserAccountIsLocked.

@Test
public void autoLoginShouldFailIfUserAccountIsLocked() {
    MockRememberMeServices services = new MockRememberMeServices(uds);
    services.setUserDetailsChecker(new AccountStatusUserDetailsChecker());
    uds.toReturn = new User("joe", "password", false, true, true, true, joe.getAuthorities());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(createLoginCookie("cookie:1:2"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    Authentication result = services.autoLogin(request, response);
    assertThat(result).isNull();
    assertCookieCancelled(response);
}
Also used : AccountStatusUserDetailsChecker(org.springframework.security.authentication.AccountStatusUserDetailsChecker) User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 5 with User

use of org.springframework.security.core.userdetails.User in project spring-security by spring-projects.

the class PreAuthenticatedAuthenticationProviderTests method authenticateUnknownUserThrowsException.

@Test(expected = UsernameNotFoundException.class)
public final void authenticateUnknownUserThrowsException() throws Exception {
    UserDetails ud = new User("dummyUser1", "dummyPwd", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new PreAuthenticatedAuthenticationToken("dummyUser2", "dummyPwd");
    provider.authenticate(request);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Aggregations

User (org.springframework.security.core.userdetails.User)54 Test (org.junit.Test)30 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)16 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)15 UserDetails (org.springframework.security.core.userdetails.UserDetails)14 Authentication (org.springframework.security.core.Authentication)13 GrantedAuthority (org.springframework.security.core.GrantedAuthority)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 ArrayList (java.util.ArrayList)5 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)4 Before (org.junit.Before)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 FilterChain (javax.servlet.FilterChain)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Assertion (org.jasig.cas.client.validation.Assertion)2 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)2