0
0
Spring Bootframework~20 mins

UserDetailsService implementation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UserDetailsService Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when loading a user by username?
Given this UserDetailsService implementation, what will be returned when calling loadUserByUsername("alice")?
Spring Boot
public class MyUserDetailsService implements UserDetailsService {
    private Map<String, UserDetails> users = Map.of(
        "alice", User.withUsername("alice").password("pass").roles("USER").build(),
        "bob", User.withUsername("bob").password("pass").roles("ADMIN").build()
    );

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (!users.containsKey(username)) {
            throw new UsernameNotFoundException("User not found");
        }
        return users.get(username);
    }
}
AThrows UsernameNotFoundException
BReturns a UserDetails object for alice with role USER
CReturns null
DReturns a UserDetails object for bob with role ADMIN
Attempts:
2 left
💡 Hint
Check if the username exists in the users map before returning.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this UserDetailsService code
Which option contains a syntax error in the UserDetailsService implementation?
Spring Boot
public class CustomUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails user = User.withUsername(username).password("pass").roles("USER").build()
        return user;
    }
}
AIncorrect method name loadUserByUser
BMissing override annotation
CMissing semicolon after build() method call
DUserDetails is not imported
Attempts:
2 left
💡 Hint
Look carefully at the end of the line with build()
🔧 Debug
advanced
2:00remaining
Why does this UserDetailsService throw UsernameNotFoundException for existing user?
Given this code, why does loadUserByUsername("John") throw UsernameNotFoundException even though john exists?
Spring Boot
public class MyUserDetailsService implements UserDetailsService {
    private Map<String, UserDetails> users = new HashMap<>();

    public MyUserDetailsService() {
        users.put("john", User.withUsername("john").password("pass").roles("USER").build());
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (users.get(username) == null) {
            throw new UsernameNotFoundException("User not found");
        }
        return users.get(username);
    }
}
AThe username passed has different case than stored key
BThe users map is immutable and cannot be modified
CThe UserDetails object is null due to missing build() call
DThe users map is empty because it is not initialized properly
Attempts:
2 left
💡 Hint
Check if the username key matches exactly in case.
state_output
advanced
2:00remaining
What roles does the returned UserDetails have?
In this UserDetailsService, what roles will the UserDetails returned by loadUserByUsername("admin") have?
Spring Boot
public class AdminUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("User not found");
        }
        return User.withUsername("admin").password("adminpass").roles("ADMIN", "USER").build();
    }
}
A["ADMIN", "USER"]
B["ADMIN"]
CEmpty list
D["USER"]
Attempts:
2 left
💡 Hint
Look at the roles method call in the return statement.
🧠 Conceptual
expert
2:00remaining
What happens if UserDetailsService returns null?
In Spring Security, what is the effect if a UserDetailsService implementation returns null instead of throwing UsernameNotFoundException when a user is not found?
AAuthentication succeeds with an anonymous user
BThe application crashes with NullPointerException
CSpring Security ignores and continues authentication
DSpring Security throws an InternalAuthenticationServiceException causing authentication failure
Attempts:
2 left
💡 Hint
Think about how Spring Security handles null UserDetails.