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 Summary
ConstructorsConstructorDescriptionCustomUserDetailsService(SecurityUtil securityUtil, org.springframework.context.ApplicationContext applicationContext) Constructs aCustomUserDetailsServicewith the required dependencies for security utilities and the Spring application context. -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.security.core.userdetails.UserDetailsloadUserByUsername(String username) Loads the user details by username.
-
Constructor Details
-
CustomUserDetailsService
public CustomUserDetailsService(SecurityUtil securityUtil, org.springframework.context.ApplicationContext applicationContext) Constructs aCustomUserDetailsServicewith 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 SpringApplicationContextused 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 withPassword.If the user is not found, a
RuntimeExceptionis thrown. If no field with thePasswordannotation is found in the user entity, anIllegalStateExceptionis thrown.- Specified by:
loadUserByUsernamein interfaceorg.springframework.security.core.userdetails.UserDetailsService- Parameters:
username- the username of the user whose details are to be loaded.- Returns:
- a
UserDetailsobject containing the user's username and password. - Throws:
org.springframework.security.core.userdetails.UsernameNotFoundException- if the user with the given username cannot be found.
-