Search in sources :

Example 31 with User

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

the class PreAuthenticatedAuthenticationProviderTests method authenticateKnownUser.

@Test
public final void authenticateKnownUser() throws Exception {
    UserDetails ud = new User("dummyUser", "dummyPwd", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new PreAuthenticatedAuthenticationToken("dummyUser", "dummyPwd");
    Authentication result = provider.authenticate(request);
    assertThat(result).isNotNull();
    assertThat(ud).isEqualTo(result.getPrincipal());
// @TODO: Add more asserts?
}
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)

Example 32 with User

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

the class PreAuthenticatedAuthenticationProviderTests method authenticateIgnoreCredentials.

@Test
public final void authenticateIgnoreCredentials() throws Exception {
    UserDetails ud = new User("dummyUser1", "dummyPwd1", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new PreAuthenticatedAuthenticationToken("dummyUser1", "dummyPwd2");
    Authentication result = provider.authenticate(request);
    assertThat(result).isNotNull();
    assertThat(ud).isEqualTo(result.getPrincipal());
// @TODO: Add more asserts?
}
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)

Example 33 with User

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

the class SecurityContextHolderAwareRequestWrapperTests method testCorrectOperationWithUserDetailsBasedPrincipal.

@Test
public void testCorrectOperationWithUserDetailsBasedPrincipal() throws Exception {
    Authentication auth = new TestingAuthenticationToken(new User("rodAsUserDetails", "koala", true, true, true, true, AuthorityUtils.NO_AUTHORITIES), "koala", "ROLE_HELLO", "ROLE_FOOBAR");
    SecurityContextHolder.getContext().setAuthentication(auth);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/");
    SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, "");
    assertThat(wrapper.getRemoteUser()).isEqualTo("rodAsUserDetails");
    assertThat(wrapper.isUserInRole("ROLE_FOO")).isFalse();
    assertThat(wrapper.isUserInRole("ROLE_NOT_GRANTED")).isFalse();
    assertThat(wrapper.isUserInRole("ROLE_FOOBAR")).isTrue();
    assertThat(wrapper.isUserInRole("ROLE_HELLO")).isTrue();
    assertThat(wrapper.getUserPrincipal()).isEqualTo(auth);
}
Also used : User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 34 with User

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

the class ClientDetailsUserDetailsService method loadUserByUsername.

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    ClientDetails clientDetails;
    try {
        clientDetails = clientDetailsService.loadClientByClientId(username);
    } catch (NoSuchClientException e) {
        throw new UsernameNotFoundException(e.getMessage(), e);
    }
    String clientSecret = clientDetails.getClientSecret();
    if (clientSecret == null || clientSecret.trim().length() == 0) {
        clientSecret = emptyPassword;
    }
    return new User(username, clientSecret, clientDetails.getAuthorities());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) User(org.springframework.security.core.userdetails.User) NoSuchClientException(org.springframework.security.oauth2.provider.NoSuchClientException)

Example 35 with User

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

the class DefaultUserAuthenticationConverterTests method shouldExtractAuthenticationWhenUserDetailsProvided.

@Test
public void shouldExtractAuthenticationWhenUserDetailsProvided() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(UserAuthenticationConverter.USERNAME, "test_user");
    UserDetailsService userDetailsService = Mockito.mock(UserDetailsService.class);
    Mockito.when(userDetailsService.loadUserByUsername("test_user")).thenReturn(new User("foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_SPAM")));
    converter.setUserDetailsService(userDetailsService);
    Authentication authentication = converter.extractAuthentication(map);
    assertEquals("ROLE_SPAM", authentication.getAuthorities().iterator().next().toString());
}
Also used : User(org.springframework.security.core.userdetails.User) HashMap(java.util.HashMap) Authentication(org.springframework.security.core.Authentication) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Aggregations

User (org.springframework.security.core.userdetails.User)56 Test (org.junit.Test)30 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)17 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 ArrayList (java.util.ArrayList)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 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