liveBook · Manning

UserDetailsManager

As explained in Spring Security in Action, Second Edition

UserDetailsManager is a Spring Security interface that extends UserDetailsService with operations for creating users and modifying or deleting user passwords.

UserDetailsManager is the Spring Security contract for applications that must manage users, not merely retrieve them during authentication. It extends UserDetailsService and adds operations for creating users, updating users, deleting users, changing passwords, and checking whether a user exists. section 3.0 section 3.3.3

public interface UserDetailsManager extends UserDetailsService {
 void createUser(UserDetails user);
 void updateUser(UserDetails user);
 void deleteUser(String username);
 void changePassword(String oldPassword, String newPassword);
 boolean userExists(String username);
}

The interface listing makes the separation concrete: authentication requires UserDetailsService, while user administration uses the additional UserDetailsManager operations. section 3.3.3

Choose the contract that matches the application’s responsibility:

  • Use UserDetailsService when the application only needs to retrieve a user by username for authentication. section 3.1
  • Use UserDetailsManager when the application also adds, modifies, deletes, or checks users. section 3.1 section 3.3.3
  • Use InMemoryUserDetailsManager for in-memory users, JdbcUserDetailsManager for SQL-backed users, or LdapUserDetailsManager for LDAP-backed users. section 3.3.3 section 3.4

Persisting users with JDBC

Register JdbcUserDetailsManager as the UserDetailsService bean and provide a DataSource. With its default queries, it expects users and authorities tables; custom queries can adapt it to different table names or schemas. A protected /hello endpoint can then be tested with curl using a stored username and password. section 3.3 section 3.3.3

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains the contract methods and demonstrates in-memory, JDBC, and LDAP implementations without descending into their internal algorithms.
Examples
Demonstrates interface methods, SQL user tables, JdbcUserDetailsManager configuration, custom queries, and LdapUserDetailsManager configuration.
What it assumes
Assumes basic Spring Security authentication and introduces the distinction between retrieving and managing user details.
What you can do after
Operate user management through an in-memory, JDBC, or LDAP implementation and verify authentication against a protected endpoint.
How it connects
Connects UserDetailsService, UserDetails, GrantedAuthority, PasswordEncoder, JDBC, SQL databases, and LDAP.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.