|
@@ -17,9 +17,12 @@
|
|
package org.springframework.security.core.userdetails;
|
|
package org.springframework.security.core.userdetails;
|
|
|
|
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.Comparator;
|
|
|
|
+import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.SortedSet;
|
|
import java.util.SortedSet;
|
|
import java.util.TreeSet;
|
|
import java.util.TreeSet;
|
|
@@ -27,6 +30,8 @@ import java.util.TreeSet;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.CredentialsContainer;
|
|
import org.springframework.security.core.CredentialsContainer;
|
|
import org.springframework.security.core.SpringSecurityCoreVersion;
|
|
import org.springframework.security.core.SpringSecurityCoreVersion;
|
|
|
|
+import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -238,4 +243,183 @@ public class User implements UserDetails, CredentialsContainer {
|
|
|
|
|
|
return sb.toString();
|
|
return sb.toString();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static UserBuilder withUsername(String username) {
|
|
|
|
+ return new UserBuilder().username(username);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Builds the user to be added. At minimum the username, password, and authorities
|
|
|
|
+ * should provided. The remaining attributes have reasonable defaults.
|
|
|
|
+ */
|
|
|
|
+ public static class UserBuilder {
|
|
|
|
+ private String username;
|
|
|
|
+ private String password;
|
|
|
|
+ private List<GrantedAuthority> authorities;
|
|
|
|
+ private boolean accountExpired;
|
|
|
|
+ private boolean accountLocked;
|
|
|
|
+ private boolean credentialsExpired;
|
|
|
|
+ private boolean disabled;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Creates a new instance
|
|
|
|
+ */
|
|
|
|
+ private UserBuilder() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the username. This attribute is required.
|
|
|
|
+ *
|
|
|
|
+ * @param username the username. Cannot be null.
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ private UserBuilder username(String username) {
|
|
|
|
+ Assert.notNull(username, "username cannot be null");
|
|
|
|
+ this.username = username;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the password. This attribute is required.
|
|
|
|
+ *
|
|
|
|
+ * @param password the password. Cannot be null.
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder password(String password) {
|
|
|
|
+ Assert.notNull(password, "password cannot be null");
|
|
|
|
+ this.password = password;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the roles. This method is a shortcut for calling
|
|
|
|
+ * {@link #authorities(String...)}, but automatically prefixes each entry with
|
|
|
|
+ * "ROLE_". This means the following:
|
|
|
|
+ *
|
|
|
|
+ * <code>
|
|
|
|
+ * builder.roles("USER","ADMIN");
|
|
|
|
+ * </code>
|
|
|
|
+ *
|
|
|
|
+ * is equivalent to
|
|
|
|
+ *
|
|
|
|
+ * <code>
|
|
|
|
+ * builder.authorities("ROLE_USER","ROLE_ADMIN");
|
|
|
|
+ * </code>
|
|
|
|
+ *
|
|
|
|
+ * <p>
|
|
|
|
+ * This attribute is required, but can also be populated with
|
|
|
|
+ * {@link #authorities(String...)}.
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @param roles the roles for this user (i.e. USER, ADMIN, etc). Cannot be null,
|
|
|
|
+ * contain null values or start with "ROLE_"
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder roles(String... roles) {
|
|
|
|
+ List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(
|
|
|
|
+ roles.length);
|
|
|
|
+ for (String role : roles) {
|
|
|
|
+ Assert.isTrue(!role.startsWith("ROLE_"), role
|
|
|
|
+ + " cannot start with ROLE_ (it is automatically added)");
|
|
|
|
+ authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
|
|
|
|
+ }
|
|
|
|
+ return authorities(authorities);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the authorities. This attribute is required.
|
|
|
|
+ *
|
|
|
|
+ * @param authorities the authorities for this user. Cannot be null, or contain
|
|
|
|
+ * null values
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ * @see #roles(String...)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder authorities(GrantedAuthority... authorities) {
|
|
|
|
+ return authorities(Arrays.asList(authorities));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the authorities. This attribute is required.
|
|
|
|
+ *
|
|
|
|
+ * @param authorities the authorities for this user. Cannot be null, or contain
|
|
|
|
+ * null values
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ * @see #roles(String...)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder authorities(List<? extends GrantedAuthority> authorities) {
|
|
|
|
+ this.authorities = new ArrayList<GrantedAuthority>(authorities);
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Populates the authorities. This attribute is required.
|
|
|
|
+ *
|
|
|
|
+ * @param authorities the authorities for this user (i.e. ROLE_USER, ROLE_ADMIN,
|
|
|
|
+ * etc). Cannot be null, or contain null values
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ * @see #roles(String...)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder authorities(String... authorities) {
|
|
|
|
+ return authorities(AuthorityUtils.createAuthorityList(authorities));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines if the account is expired or not. Default is false.
|
|
|
|
+ *
|
|
|
|
+ * @param accountExpired true if the account is expired, false otherwise
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder accountExpired(boolean accountExpired) {
|
|
|
|
+ this.accountExpired = accountExpired;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines if the account is locked or not. Default is false.
|
|
|
|
+ *
|
|
|
|
+ * @param accountLocked true if the account is locked, false otherwise
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder accountLocked(boolean accountLocked) {
|
|
|
|
+ this.accountLocked = accountLocked;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines if the credentials are expired or not. Default is false.
|
|
|
|
+ *
|
|
|
|
+ * @param credentialsExpired true if the credentials are expired, false otherwise
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder credentialsExpired(boolean credentialsExpired) {
|
|
|
|
+ this.credentialsExpired = credentialsExpired;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines if the account is disabled or not. Default is false.
|
|
|
|
+ *
|
|
|
|
+ * @param disabled true if the account is disabled, false otherwise
|
|
|
|
+ * @return the {@link UserBuilder} for method chaining (i.e. to populate
|
|
|
|
+ * additional attributes for this user)
|
|
|
|
+ */
|
|
|
|
+ public UserBuilder disabled(boolean disabled) {
|
|
|
|
+ this.disabled = disabled;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public UserDetails build() {
|
|
|
|
+ return new User(username, password, !disabled, !accountExpired,
|
|
|
|
+ !credentialsExpired, !accountLocked, authorities);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|