Search in sources :

Example 41 with User

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

the class UsernamePasswordAuthenticationTokenMixinTests method deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest.

@Test
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest() throws IOException {
    UsernamePasswordAuthenticationToken token = mapper.readValue(AUTHENTICATED_JSON, UsernamePasswordAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
    assertThat(((User) token.getPrincipal()).getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(token.isAuthenticated()).isEqualTo(true);
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 42 with User

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

the class JdbcUserDetailsManagerTests method updateUserChangesDataCorrectlyAndClearsCache.

@Test
public void updateUserChangesDataCorrectlyAndClearsCache() {
    insertJoe();
    User newJoe = new User("joe", "newpassword", false, true, true, true, AuthorityUtils.createAuthorityList(new String[] { "D", "F", "E" }));
    manager.updateUser(newJoe);
    UserDetails joe = manager.loadUserByUsername("joe");
    assertThat(joe).isEqualTo(newJoe);
    assertThat(cache.getUserMap().containsKey("joe")).isFalse();
}
Also used : User(org.springframework.security.core.userdetails.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) Test(org.junit.Test)

Example 43 with User

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

the class AnonymousAuthenticationTokenMixinTests method serializeAnonymousAuthenticationTokenMixinAfterEraseCredentialTest.

@Test
public void serializeAnonymousAuthenticationTokenMixinAfterEraseCredentialTest() throws JsonProcessingException, JSONException {
    User user = createDefaultUser();
    AnonymousAuthenticationToken token = new AnonymousAuthenticationToken(HASH_KEY, user, user.getAuthorities());
    token.eraseCredentials();
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(ANONYMOUS_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
}
Also used : User(org.springframework.security.core.userdetails.User) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 44 with User

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

the class RememberMeAuthenticationTokenMixinTests method deserializeRememberMeAuthenticationTokenWithUserTest.

@Test
public void deserializeRememberMeAuthenticationTokenWithUserTest() throws IOException {
    RememberMeAuthenticationToken token = mapper.readValue(String.format(REMEMBERME_AUTH_JSON, "\"password\""), RememberMeAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
    assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin");
    assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234");
    assertThat(((User) token.getPrincipal()).getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(((User) token.getPrincipal()).isEnabled()).isEqualTo(true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 45 with User

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

the class AuthenticationPrincipalArgumentResolverTests method resolveArgumentUserDetails.

@Test
public void resolveArgumentUserDetails() throws Exception {
    setAuthenticationPrincipal(new User("user", "password", AuthorityUtils.createAuthorityList("ROLE_USER")));
    assertThat(resolver.resolveArgument(showUserAnnotationUserDetails(), null)).isEqualTo(expectedPrincipal);
}
Also used : User(org.springframework.security.core.userdetails.User) 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