Class User.UserBuilder

java.lang.Object
org.springframework.security.core.userdetails.User.UserBuilder
Enclosing class:
User

public static final class User.UserBuilder extends Object
Builds the user to be added. At minimum the username, password, and authorities should provided. The remaining attributes have reasonable defaults.
  • Method Details

    • username

      public User.UserBuilder username(String username)
      Populates the username. This attribute is required.
      Parameters:
      username - the username. Cannot be null.
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • password

      public User.UserBuilder password(String password)
      Populates the password. This attribute is required.
      Parameters:
      password - the password. Cannot be null.
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • passwordEncoder

      public User.UserBuilder passwordEncoder(Function<String,String> encoder)
      Encodes the current password (if non-null) and any future passwords supplied to password(String).
      Parameters:
      encoder - the encoder to use
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • roles

      public User.UserBuilder roles(String... roles)
      Populates the roles. This method is a shortcut for calling authorities(String...), but automatically prefixes each entry with "ROLE_". This means the following: builder.roles("USER","ADMIN"); is equivalent to builder.authorities("ROLE_USER","ROLE_ADMIN");

      This attribute is required, but can also be populated with authorities(String...).

      Parameters:
      roles - the roles for this user (i.e. USER, ADMIN, etc). Cannot be null, contain null values or start with "ROLE_"
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • authorities

      public User.UserBuilder authorities(GrantedAuthority... authorities)
      Populates the authorities. This attribute is required.
      Parameters:
      authorities - the authorities for this user. Cannot be null, or contain null values
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
      See Also:
    • authorities

      public User.UserBuilder authorities(Collection<? extends GrantedAuthority> authorities)
      Populates the authorities. This attribute is required.
      Parameters:
      authorities - the authorities for this user. Cannot be null, or contain null values
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
      See Also:
    • authorities

      public User.UserBuilder authorities(String... authorities)
      Populates the authorities. This attribute is required.
      Parameters:
      authorities - the authorities for this user (i.e. ROLE_USER, ROLE_ADMIN, etc). Cannot be null, or contain null values
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
      See Also:
    • accountExpired

      public User.UserBuilder accountExpired(boolean accountExpired)
      Defines if the account is expired or not. Default is false.
      Parameters:
      accountExpired - true if the account is expired, false otherwise
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • accountLocked

      public User.UserBuilder accountLocked(boolean accountLocked)
      Defines if the account is locked or not. Default is false.
      Parameters:
      accountLocked - true if the account is locked, false otherwise
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • credentialsExpired

      public User.UserBuilder credentialsExpired(boolean credentialsExpired)
      Defines if the credentials are expired or not. Default is false.
      Parameters:
      credentialsExpired - true if the credentials are expired, false otherwise
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • disabled

      public User.UserBuilder disabled(boolean disabled)
      Defines if the account is disabled or not. Default is false.
      Parameters:
      disabled - true if the account is disabled, false otherwise
      Returns:
      the User.UserBuilder for method chaining (i.e. to populate additional attributes for this user)
    • build

      public UserDetails build()