Complete the code to declare the UserDetailsService implementation class.
public class [1] implements UserDetailsService { // implementation }
The class implementing UserDetailsService should have a clear name like MyUserDetailsService.
Complete the method signature to override loadUserByUsername.
@Override
public [1] loadUserByUsername(String username) throws UsernameNotFoundException {
// method body
}The method loadUserByUsername must return a UserDetails object.
Fix the error in fetching user by username from repository.
User user = userRepository.[1](username); if (user == null) { throw new UsernameNotFoundException("User not found"); }
The repository method to find user by username is usually named findByUsername.
Fill both blanks to return a UserDetails object with username and password.
return new org.springframework.security.core.userdetails.User([1], [2], new ArrayList<>());
The UserDetails constructor needs username and password from the user object.
Fill all three blanks to complete the UserDetailsService implementation with repository injection and method.
public class MyUserDetailsService implements UserDetailsService { private final [1] userRepository; public MyUserDetailsService([2] userRepository) { this.userRepository = userRepository; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.[3](username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>()); } }
The repository is injected via constructor and used to find user by username.