Class UserPrincipal

java.lang.Object
me.josephsf.jobportaljosephsfeir.security.UserPrincipal
All Implemented Interfaces:
Serializable, org.springframework.security.core.userdetails.UserDetails

public class UserPrincipal extends Object implements org.springframework.security.core.userdetails.UserDetails
Custom implementation of Spring Security's UserDetails interface.

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 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 identifier
      username - the user's username
      email - the user's email
      password - the user's encoded password
      authorities - the authorities granted to the user
  • Method Details

    • build

      public static UserPrincipal build(User user)
      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

      public Collection<? extends org.springframework.security.core.GrantedAuthority> 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:
      getAuthorities in interface org.springframework.security.core.userdetails.UserDetails
      Returns:
      the authorities granted to the user
    • getId

      public String getId()
      Gets the user's unique identifier.
      Returns:
      the user ID
    • getEmail

      public String getEmail()
      Gets the user's email address.
      Returns:
      the user's email
    • getPassword

      public String getPassword()
      Gets the user's password.
      Specified by:
      getPassword in interface org.springframework.security.core.userdetails.UserDetails
      Returns:
      the user's password
    • getUsername

      public String getUsername()
      Gets the user's username.
      Specified by:
      getUsername in interface org.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:
      isAccountNonExpired in interface org.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:
      isAccountNonLocked in interface org.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:
      isCredentialsNonExpired in interface org.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:
      isEnabled in interface org.springframework.security.core.userdetails.UserDetails
      Returns:
      true if the user is enabled (always true in this implementation)