mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-14 14:23:30 +00:00
Add UserBuilders.withUserDetails
This commit is contained in:
parent
14f5ebcc3b
commit
051e3fb079
@ -248,6 +248,16 @@ public class User implements UserDetails, CredentialsContainer {
|
|||||||
return new UserBuilder().username(username);
|
return new UserBuilder().username(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UserBuilder withUserDetails(UserDetails userDetails) {
|
||||||
|
return withUsername(userDetails.getUsername())
|
||||||
|
.password(userDetails.getPassword())
|
||||||
|
.accountExpired(!userDetails.isAccountNonExpired())
|
||||||
|
.accountLocked(!userDetails.isAccountNonLocked())
|
||||||
|
.authorities(userDetails.getAuthorities())
|
||||||
|
.credentialsExpired(!userDetails.isCredentialsNonExpired())
|
||||||
|
.disabled(!userDetails.isEnabled());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds the user to be added. At minimum the username, password, and authorities
|
* Builds the user to be added. At minimum the username, password, and authorities
|
||||||
* should provided. The remaining attributes have reasonable defaults.
|
* should provided. The remaining attributes have reasonable defaults.
|
||||||
@ -351,7 +361,7 @@ public class User implements UserDetails, CredentialsContainer {
|
|||||||
* additional attributes for this user)
|
* additional attributes for this user)
|
||||||
* @see #roles(String...)
|
* @see #roles(String...)
|
||||||
*/
|
*/
|
||||||
public UserBuilder authorities(List<? extends GrantedAuthority> authorities) {
|
public UserBuilder authorities(Collection<? extends GrantedAuthority> authorities) {
|
||||||
this.authorities = new ArrayList<GrantedAuthority>(authorities);
|
this.authorities = new ArrayList<GrantedAuthority>(authorities);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -146,4 +146,35 @@ public class UserTests {
|
|||||||
out.writeObject(user);
|
out.writeObject(user);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withUserDetailsWhenAllEnabled() throws Exception {
|
||||||
|
User expected = new User("rob","pass", true, true, true, true, ROLE_12);
|
||||||
|
|
||||||
|
UserDetails actual = User.withUserDetails(expected).build();
|
||||||
|
|
||||||
|
assertThat(actual.getUsername()).isEqualTo(expected.getUsername());
|
||||||
|
assertThat(actual.getPassword()).isEqualTo(expected.getPassword());
|
||||||
|
assertThat(actual.getAuthorities()).isEqualTo(expected.getAuthorities());
|
||||||
|
assertThat(actual.isAccountNonExpired()).isEqualTo(expected.isAccountNonExpired());
|
||||||
|
assertThat(actual.isAccountNonLocked()).isEqualTo(expected.isAccountNonLocked());
|
||||||
|
assertThat(actual.isCredentialsNonExpired()).isEqualTo(expected.isCredentialsNonExpired());
|
||||||
|
assertThat(actual.isEnabled()).isEqualTo(expected.isEnabled());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withUserDetailsWhenAllDisabled() throws Exception {
|
||||||
|
User expected = new User("rob","pass", false, false, false, false, ROLE_12);
|
||||||
|
|
||||||
|
UserDetails actual = User.withUserDetails(expected).build();
|
||||||
|
|
||||||
|
assertThat(actual.getUsername()).isEqualTo(expected.getUsername());
|
||||||
|
assertThat(actual.getPassword()).isEqualTo(expected.getPassword());
|
||||||
|
assertThat(actual.getAuthorities()).isEqualTo(expected.getAuthorities());
|
||||||
|
assertThat(actual.isAccountNonExpired()).isEqualTo(expected.isAccountNonExpired());
|
||||||
|
assertThat(actual.isAccountNonLocked()).isEqualTo(expected.isAccountNonLocked());
|
||||||
|
assertThat(actual.isCredentialsNonExpired()).isEqualTo(expected.isCredentialsNonExpired());
|
||||||
|
assertThat(actual.isEnabled()).isEqualTo(expected.isEnabled());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user