Class User
- All Implemented Interfaces:
Serializable,CredentialsContainer,UserDetails
UserDetailsService.
Developers may use this class directly, subclass it, or write their own
UserDetails implementation from scratch.
equals and hashcode implementations are based on the username
property only, as the intention is that lookups of the same user principal object (in a
user registry, for example) will match where the objects represent the same user, not
just when all the properties (authorities, password for example) are the same.
Note that this implementation is not immutable. It implements the
CredentialsContainer interface, in order to allow the password to be erased
after authentication. This may cause side-effects if you are storing instances
in-memory and reusing them. If so, make sure you return a copy from your
UserDetailsService each time it is invoked.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilds the user to be added. -
Constructor Summary
ConstructorsConstructorDescriptionUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) Construct theUserwith the details required byDaoAuthenticationProvider.User(String username, String password, Collection<? extends GrantedAuthority> authorities) Calls the more complex constructor with all boolean arguments set totrue. -
Method Summary
Modifier and TypeMethodDescriptionstatic User.UserBuilderbuilder()Creates a UserBuilderbooleanReturnstrueif the supplied object is aUserinstance with the sameusernamevalue.voidReturns the authorities granted to the user.Returns the password used to authenticate the user.Returns the username used to authenticate the user.inthashCode()Returns the hashcode of theusername.booleanIndicates whether the user's account has expired.booleanIndicates whether the user is locked or unlocked.booleanIndicates whether the user's credentials (password) has expired.booleanIndicates whether the user is enabled or disabled.toString()static User.UserBuilderDeprecated.Using this method is not considered safe for production, but is acceptable for demos and getting started.static User.UserBuilderwithUserDetails(UserDetails userDetails) static User.UserBuilderwithUsername(String username) Creates a UserBuilder with a specified username
-
Constructor Details
-
User
Calls the more complex constructor with all boolean arguments set totrue. -
User
public User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) Construct theUserwith the details required byDaoAuthenticationProvider.- Parameters:
username- the username presented to theDaoAuthenticationProviderpassword- the password that should be presented to theDaoAuthenticationProviderenabled- set totrueif the user is enabledaccountNonExpired- set totrueif the account has not expiredcredentialsNonExpired- set totrueif the credentials have not expiredaccountNonLocked- set totrueif the account is not lockedauthorities- the authorities that should be granted to the caller if they presented the correct username and password and the user is enabled. Not null.- Throws:
IllegalArgumentException- if anullvalue was passed either as a parameter or as an element in theGrantedAuthoritycollection
-
-
Method Details
-
getAuthorities
Description copied from interface:UserDetailsReturns the authorities granted to the user. Cannot returnnull.- Specified by:
getAuthoritiesin interfaceUserDetails- Returns:
- the authorities, sorted by natural key (never
null)
-
getPassword
Description copied from interface:UserDetailsReturns the password used to authenticate the user.- Specified by:
getPasswordin interfaceUserDetails- Returns:
- the password
-
getUsername
Description copied from interface:UserDetailsReturns the username used to authenticate the user. Cannot returnnull.- Specified by:
getUsernamein interfaceUserDetails- Returns:
- the username (never
null)
-
isEnabled
public boolean isEnabled()Description copied from interface:UserDetailsIndicates whether the user is enabled or disabled. A disabled user cannot be authenticated.- Specified by:
isEnabledin interfaceUserDetails- Returns:
trueif the user is enabled,falseotherwise
-
isAccountNonExpired
public boolean isAccountNonExpired()Description copied from interface:UserDetailsIndicates whether the user's account has expired. An expired account cannot be authenticated.- Specified by:
isAccountNonExpiredin interfaceUserDetails- Returns:
trueif the user's account is valid (ie non-expired),falseif no longer valid (ie expired)
-
isAccountNonLocked
public boolean isAccountNonLocked()Description copied from interface:UserDetailsIndicates whether the user is locked or unlocked. A locked user cannot be authenticated.- Specified by:
isAccountNonLockedin interfaceUserDetails- Returns:
trueif the user is not locked,falseotherwise
-
isCredentialsNonExpired
public boolean isCredentialsNonExpired()Description copied from interface:UserDetailsIndicates whether the user's credentials (password) has expired. Expired credentials prevent authentication.- Specified by:
isCredentialsNonExpiredin interfaceUserDetails- Returns:
trueif the user's credentials are valid (ie non-expired),falseif no longer valid (ie expired)
-
eraseCredentials
public void eraseCredentials()- Specified by:
eraseCredentialsin interfaceCredentialsContainer
-
equals
Returnstrueif the supplied object is aUserinstance with the sameusernamevalue.In other words, the objects are equal if they have the same username, representing the same principal.
-
hashCode
public int hashCode()Returns the hashcode of theusername. -
toString
-
withUsername
Creates a UserBuilder with a specified username- Parameters:
username- the username to use- Returns:
- the UserBuilder
-
builder
Creates a UserBuilder- Returns:
- the UserBuilder
-
withDefaultPasswordEncoder
Deprecated.Using this method is not considered safe for production, but is acceptable for demos and getting started. For production purposes, ensure the password is encoded externally. See the method Javadoc for additional details. There are no plans to remove this support. It is deprecated to indicate that this is considered insecure for production purposes.WARNING: This method is considered unsafe for production and is only intended for sample applications.
Creates a user and automatically encodes the provided password using
PasswordEncoderFactories.createDelegatingPasswordEncoder(). For example:
This is not safe for production (it is intended for getting started experience) because the password "password" is compiled into the source code and then is included in memory at the time of creation. This means there are still ways to recover the plain text password making it unsafe. It does provide a slight improvement to using plain text passwords since the UserDetails password is securely hashed. This means if the UserDetails password is accidentally exposed, the password is securely stored. In a production setting, it is recommended to hash the password ahead of time. For example:UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); // outputs {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG System.out.println(user.getPassword());PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); // outputs {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG // remember the password that is printed out and use in the next step System.out.println(encoder.encode("password"));UserDetails user = User.withUsername("user") .password("{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG") .roles("USER") .build();- Returns:
- a UserBuilder that automatically encodes the password with the default PasswordEncoder
-
withUserDetails
-