This post will discuss how to resolve java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" exception in a Spring Boot application.

Until Spring Security 4, it was possible to store passwords in plain text using in-memory authentication. For instance, the following code runs fine with Spring Security 4:

 
Spring Security 5 recommends using the PasswordEncoder interface for encoding passwords. So, if we upgrade our application to Spring Security 5, we’ll end up with the following error for the above code:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:244) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$LazyPasswordEncoder.matches(AuthenticationConfiguration.java:289) ~[spring-security-config-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:90) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) ~[spring-security-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]

The default PasswordEncoder is built as a DelegatingPasswordEncoder in Spring Security 5, but no password encoder was configured for our in-memory authentication. This results in the above error when the passwords are stored in plain text inside the memory. To resolve the above error, we should configure in-memory authentication to use a PasswordEncoder.

1. We can force DelegatingPasswordEncoder to use plain text simply by prefix {noop} to our passwords. This will activate the NoOpPasswordEncoder instead of the default DelegatingPasswordEncoder.

 
Alternatively, we can pass instance of NoOpPasswordEncoder to passwordEncoder() method of InMemoryUserDetailsManagerConfigurer class, as shown below:

 
2. We can also use included encoders in PasswordEncoderFactories or define our own set of password encoders. The following example creates a DelegatingPasswordEncoder with default mappings.