Class UserDetailsServiceImpl

java.lang.Object
me.josephsf.jobportaljosephsfeir.security.UserDetailsServiceImpl
All Implemented Interfaces:
org.springframework.security.core.userdetails.UserDetailsService

@Service public class UserDetailsServiceImpl extends Object implements org.springframework.security.core.userdetails.UserDetailsService
Implementation of Spring Security's UserDetailsService for authentication.

This service provides the core user authentication functionality by loading user-specific data. It implements the standard Spring Security UserDetailsService interface which is required by the authentication provider. When a user attempts to authenticate, this service is used to load the user's details from the database.

The implementation:

  • Retrieves a user from the database based on the provided username
  • Converts the domain User object to a Spring Security UserDetails object
  • Throws an exception if the user is not found

This service acts as a bridge between the Spring Security authentication system and the application's user database, enabling username/password authentication against the MongoDB user repository.

Since:
2025-05-15
See Also:
  • Constructor Details

    • UserDetailsServiceImpl

      public UserDetailsServiceImpl(UserRepository userRepository)
      Constructor with dependency injection.
      Parameters:
      userRepository - the repository for accessing user data
  • Method Details

    • loadUserByUsername

      @Transactional public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException
      Loads a user's details by username.

      This method is called by Spring Security during the authentication process. It retrieves the user from the database and converts it to a UserDetails object that Spring Security can use for authentication and authorization.

      Specified by:
      loadUserByUsername in interface org.springframework.security.core.userdetails.UserDetailsService
      Parameters:
      username - the username identifying the user whose data is required
      Returns:
      a fully populated UserDetails object
      Throws:
      org.springframework.security.core.userdetails.UsernameNotFoundException - if the user cannot be found