0
0
Spring Bootframework~10 mins

UserDetailsService implementation in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the UserDetailsService implementation class.

Spring Boot
public class [1] implements UserDetailsService {
    // implementation
}
Drag options to blanks, or click blank then click option'
AUserDetails
BUserService
CMyUserDetailsService
DUserRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not reflect its purpose.
Not implementing UserDetailsService interface.
2fill in blank
medium

Complete the method signature to override loadUserByUsername.

Spring Boot
@Override
public [1] loadUserByUsername(String username) throws UsernameNotFoundException {
    // method body
}
Drag options to blanks, or click blank then click option'
Avoid
BUserDetails
CUser
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void or wrong type.
Missing the @Override annotation.
3fill in blank
hard

Fix the error in fetching user by username from repository.

Spring Boot
User user = userRepository.[1](username);
if (user == null) {
    throw new UsernameNotFoundException("User not found");
}
Drag options to blanks, or click blank then click option'
AfindByUsername
BfindById
CgetUser
DloadUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using findById which expects an ID, not username.
Using non-existent methods like getUser or loadUser.
4fill in blank
hard

Fill both blanks to return a UserDetails object with username and password.

Spring Boot
return new org.springframework.security.core.userdetails.User([1], [2], new ArrayList<>());
Drag options to blanks, or click blank then click option'
Auser.getUsername()
Buser.getPassword()
Cuser.getEmail()
Duser.getRoles()
Attempts:
3 left
💡 Hint
Common Mistakes
Using email instead of username.
Using roles where password is expected.
5fill in blank
hard

Fill all three blanks to complete the UserDetailsService implementation with repository injection and method.

Spring Boot
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<>());
    }
}
Drag options to blanks, or click blank then click option'
AUserRepository
CfindByUsername
DUserService
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong repository type or service class.
Using incorrect method names for fetching user.