Challenge - 5 Problems
UserDetailsService Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check if the username exists in the users map before returning.
✗ Incorrect
The method checks if the username exists in the map. For "alice", it finds the user and returns the UserDetails with role USER.
📝 Syntax
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Look carefully at the end of the line with build()
✗ Incorrect
The line calling build() is missing a semicolon at the end, causing a syntax error.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Check if the username key matches exactly in case.
✗ Incorrect
The map key is "john" lowercase, but if the input username has different case like "John", the get returns null causing the exception.
❓ state_output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Look at the roles method call in the return statement.
✗ Incorrect
The roles method is called with "ADMIN" and "USER", so both roles are assigned.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how Spring Security handles null UserDetails.
✗ Incorrect
Returning null causes Spring Security to throw InternalAuthenticationServiceException, failing authentication cleanly.