Package vaultWeb.services.auth
Class MyUserDetailsService
java.lang.Object
vaultWeb.services.auth.MyUserDetailsService
- All Implemented Interfaces:
org.springframework.security.core.userdetails.UserDetailsService
@Service
public class MyUserDetailsService
extends Object
implements org.springframework.security.core.userdetails.UserDetailsService
Service class that integrates the application's User entity with Spring Security.
Implements UserDetailsService, which is used by Spring Security during the
authentication process. This service loads user-specific data given a username.
Responsibilities:
- Retrieve a
Userentity from the database viaUserRepository. - Convert the domain
Userinto a Spring SecurityUserDetailsobject. - Throw
UsernameNotFoundExceptionif the user does not exist, signaling authentication failure.
Detailed workflow:
- Spring Security calls
loadUserByUsername(String)with the username supplied during login. - The method queries the
UserRepositoryto fetch theUserentity. - If no user is found, a
UsernameNotFoundExceptionis thrown. - If the user is found, a
UserDetailsobject is built using:- username: used in the security context as the principal
- password: hashed password stored in the database (e.g., BCrypt)
- authorities: roles or permissions (can be added here if needed)
- Spring Security then compares the provided plaintext password with the stored hash using
the configured
PasswordEncoder. - If the passwords match, authentication succeeds; otherwise, it fails.
This design decouples the application's User entity from Spring Security's internal
representation, providing flexibility and security abstraction.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.security.core.userdetails.UserDetailsloadUserByUsername(String username) Loads the user details for Spring Security based on the given username.
-
Constructor Details
-
MyUserDetailsService
public MyUserDetailsService()
-
-
Method Details
-
loadUserByUsername
public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException Loads the user details for Spring Security based on the given username.- Specified by:
loadUserByUsernamein interfaceorg.springframework.security.core.userdetails.UserDetailsService- Parameters:
username- The username of the user attempting to authenticate.- Returns:
- A
UserDetailsobject containing the username, hashed password, and authorities. - Throws:
org.springframework.security.core.userdetails.UsernameNotFoundException- if the username does not exist in the database.
-