liveBook · Manning

JdbcUserDetailsManager

As explained in Spring Security in Action, Second Edition

JdbcUserDetailsManager is a Spring Security UserDetailsService and UserDetailsManager implementation that stores and manages user details through JDBC and a relational database.

JdbcUserDetailsManager implements UserDetailsService and UserDetailsManager, retrieving and managing Spring Security users directly through JDBC and a relational database. section 3.3.3 section 3.4

Authentication uses the inherited user lookup contract; user administration adds operations for creating, updating, deleting, checking, and changing user passwords. section 3.3.3 section 3.0

A basic JDBC arrangement uses the following pieces:

  • Create users and authorities tables, using the default table names recognized by JdbcUserDetailsManager. section 3.3.3
  • Store username, password, and enabled in users; store username and authority in authorities. section 3.3.3
  • Expose a UserDetailsService bean that constructs JdbcUserDetailsManager with a DataSource. section 3.3.3
  • Override the users and authorities queries when an existing schema does not match the defaults. section 3.3.3

Minimal configuration and verification

Register `new JdbcUserDetailsManager(dataSource)` as the UserDetailsService, configure a PasswordEncoder, and protect a `/hello` endpoint. With a stored `john` user, `curl -u john:12345 http://localhost:8080/hello` returns `Hello!`. 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);
}

Inspect the UserDetailsManager contract to see the inherited lookup role alongside user-creation and account-management operations. section 3.3.3

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains interfaces, default tables, required columns, bean configuration, query overrides, and an authenticated endpoint test.
Examples
Uses users and authorities SQL tables, a MySQL database, an H2 setup, custom queries, and curl authentication against /hello.
What it assumes
Assumes basic Spring Security contracts and JDBC configuration while defining UserDetailsService and UserDetailsManager responsibilities.
What you can do after
Operate/configure Spring Security authentication against JDBC users and adapt lookup queries to an existing SQL schema.
How it connects
Connects UserDetailsService, UserDetailsManager, UserDetails, GrantedAuthority, DataSource, PasswordEncoder, and relational databases.

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.