liveBook · Manning

PasswordEncoder

As explained in Spring Security in Action, Second Edition

PasswordEncoder is a Spring Security strategy for encoding passwords and matching supplied passwords against stored representations.

PasswordEncoder is a Spring Security strategy that encodes passwords and checks whether supplied passwords match stored representations. AuthenticationProvider uses it during authentication, and Basic authentication requires one. section 2.2 section 4.1

The contract centers on three operations:

  • encode(CharSequence rawPassword) transforms a supplied password into its stored representation, typically through encryption or hashing. section 4.1.1
  • matches(CharSequence rawPassword, String encodedPassword) validates a supplied password against a known representation. section 4.1.1
  • upgradeEncoding(String encodedPassword) defaults to false and can signal that an existing representation should be encoded again. section 4.1.1
  • Keep encode and matches consistent: every result from encode should be verifiable by matches on the same implementation. section 4.1.2

Spring Security can use a PasswordEncoder bean alongside UserDetailsService. Defining users without an encoder can produce “There is no PasswordEncoder mapped for the id \"null\"”; an explicit NoOpPasswordEncoder makes the sample work but stores cleartext and is unsuitable for production. section 2.3.1 section 2.4

Registering an encoder bean

A configuration class can expose PasswordEncoder as a bean, while UserDetailsService manages an in-memory user. The encoder may also be registered locally through SecurityFilterChain when no other component needs injection. section 2.3.1 section 2.3.3

Figure 4.1 The Spring Security authentication process. The AuthenticationProvider uses the PasswordEncoder to validate the user’s password in the authentication process.

Use the authentication-process figure to trace how AuthenticationProvider invokes PasswordEncoder to validate a user password. section 4.1

Available implementations include PBKDF2, BCrypt, and SCrypt. NoOpPasswordEncoder leaves passwords cleartext, while StandardPasswordEncoder uses deprecated SHA-256. DelegatingPasswordEncoder selects an implementation from a prefix such as {bcrypt}, allowing algorithms to coexist during migrations. section 4.1.3 section 4.1.4

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines encode, matches, and upgradeEncoding, implements plain-text and SHA-512 examples, then compares framework-provided strategies.
Examples
Uses NoOpPasswordEncoder, custom plain-text and SHA-512 implementations, BCrypt and SCrypt choices, and prefixed delegation.
What it assumes
Assumes basic Spring configuration and authentication concepts while introducing password encoding, matching, and implementation choices.
What you can do after
Build it from scratch by implementing encode and matches, configure a Spring bean, or migrate algorithms with DelegatingPasswordEncoder.
How it connects
Connects AuthenticationProvider, UserDetailsService, SecurityFilterChain, DelegatingPasswordEncoder, and password hashing.

Also explained in

Compare how 2 books explain PasswordEncoder →

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.