Class CustomUserDetailsService

java.lang.Object
tech.lastbox.lastshield.security.core.CustomUserDetailsService
All Implemented Interfaces:
org.springframework.security.core.userdetails.UserDetailsService

@Service public class CustomUserDetailsService extends Object implements org.springframework.security.core.userdetails.UserDetailsService
CustomUserDetailsService is an implementation of UserDetailsService that loads user-specific data during authentication. It fetches user details from a dynamically determined user repository, using the username to identify the user and retrieving the user's password from a field annotated with Password.

This service uses Spring's ApplicationContext to retrieve the appropriate user repository based on the configuration provided by SecurityUtil. The password is extracted from a field in the user entity class that is annotated with Password.

  • Constructor Details

    • CustomUserDetailsService

      public CustomUserDetailsService(SecurityUtil securityUtil, org.springframework.context.ApplicationContext applicationContext)
      Constructs a CustomUserDetailsService with the required dependencies for security utilities and the Spring application context. This constructor uses Spring's Dependency Injection mechanism to inject the necessary beans into the class.
      Parameters:
      securityUtil - the utility class for security-related operations, such as finding users by username.
      applicationContext - the Spring ApplicationContext used to retrieve the user repository bean.
  • Method Details

    • loadUserByUsername

      public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException
      Loads the user details by username. This method fetches the user entity from the user repository and retrieves the password field that is annotated with Password.

      If the user is not found, a RuntimeException is thrown. If no field with the Password annotation is found in the user entity, an IllegalStateException is thrown.

      Specified by:
      loadUserByUsername in interface org.springframework.security.core.userdetails.UserDetailsService
      Parameters:
      username - the username of the user whose details are to be loaded.
      Returns:
      a UserDetails object containing the user's username and password.
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException - if the user with the given username cannot be found.