Class UserPrincipal
- All Implemented Interfaces:
Serializable,org.springframework.security.core.userdetails.UserDetails
This class serves as an adapter between the application's User domain model and Spring Security's UserDetails interface. It wraps a User entity and provides the necessary methods required by Spring Security for authentication and authorization, such as credentials, authorities (roles), and account status flags.
UserPrincipal contains:
- User identification information (ID, username, email)
- Authentication credentials (password)
- Granted authorities (roles converted to Spring Security's GrantedAuthority)
- Account status flags (account expiration, lock status, credentials expiration, enabled status)
This class is used by the authentication system to represent the currently authenticated user and to check permissions for secured operations.
- Since:
- 2025-05-15
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionUserPrincipal(String id, String username, String email, String password, Collection<? extends org.springframework.security.core.GrantedAuthority> authorities) Constructs a UserPrincipal with the specified details. -
Method Summary
Modifier and TypeMethodDescriptionstatic UserPrincipalCreates a UserPrincipal from a User entity.Collection<? extends org.springframework.security.core.GrantedAuthority> Returns the authorities granted to the user.getEmail()Gets the user's email address.getId()Gets the user's unique identifier.Gets the user's password.Gets the user's username.booleanIndicates whether the user's account has expired.booleanIndicates whether the user's account is locked.booleanIndicates whether the user's credentials (password) have expired.booleanIndicates whether the user is enabled.
-
Constructor Details
-
UserPrincipal
public UserPrincipal(String id, String username, String email, String password, Collection<? extends org.springframework.security.core.GrantedAuthority> authorities) Constructs a UserPrincipal with the specified details.- Parameters:
id- the user's unique identifierusername- the user's usernameemail- the user's emailpassword- the user's encoded passwordauthorities- the authorities granted to the user
-
-
Method Details
-
build
Creates a UserPrincipal from a User entity.This factory method converts the application's User model to a Spring Security UserDetails implementation. It transforms the User's roles into GrantedAuthority objects with the "ROLE_" prefix required by Spring Security.
- Parameters:
user- the User entity to convert- Returns:
- a UserPrincipal representing the specified user
-
getAuthorities
Returns the authorities granted to the user.These authorities (roles) are used by Spring Security for authorization checks, such as method security or URL-based security.
- Specified by:
getAuthoritiesin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- the authorities granted to the user
-
getId
Gets the user's unique identifier.- Returns:
- the user ID
-
getEmail
Gets the user's email address.- Returns:
- the user's email
-
getPassword
Gets the user's password.- Specified by:
getPasswordin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- the user's password
-
getUsername
Gets the user's username.- Specified by:
getUsernamein interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- the user's username
-
isAccountNonExpired
public boolean isAccountNonExpired()Indicates whether the user's account has expired.An expired account cannot be authenticated.
- Specified by:
isAccountNonExpiredin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- true if the account is not expired (always true in this implementation)
-
isAccountNonLocked
public boolean isAccountNonLocked()Indicates whether the user's account is locked.A locked account cannot be authenticated.
- Specified by:
isAccountNonLockedin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- true if the account is not locked (always true in this implementation)
-
isCredentialsNonExpired
public boolean isCredentialsNonExpired()Indicates whether the user's credentials (password) have expired.Expired credentials prevent authentication.
- Specified by:
isCredentialsNonExpiredin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- true if the credentials are not expired (always true in this implementation)
-
isEnabled
public boolean isEnabled()Indicates whether the user is enabled.A disabled user cannot be authenticated.
- Specified by:
isEnabledin interfaceorg.springframework.security.core.userdetails.UserDetails- Returns:
- true if the user is enabled (always true in this implementation)
-