There is no PasswordEncoder mapped for the id "null": the simplest solution

created at 12-20-2021 views: 41


After SpringSecurity5.x, when verifying the password, you need to specify the encryption method;

I found out after reading the official documents.


When you pass the password, splice a string in front of the password (if you use other encryption methods, just splice the corresponding string);

like this:

String password = "{bcrypt}" + "Encrypted password";

I believe that most of you have this problem when you take the password of the database, so I won't talk about the memory authentication based on it. If there is a need, there are examples in the document. Look at it and you will understand.

Actual sample code:

     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {"Username{}", username);
         if (username == null) {
             throw new UsernameNotFoundException("Username or password cannot be empty!");

         UserRoleBean userRoleBean = userRoleDao.login(username);
         if (userRoleBean == null) {
             throw new UsernameNotFoundException("Username or password is wrong!");
         return new User(username, "{bcrypt}" + userRoleBean.getUsers().getUpwd(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_" + userRoleBean.getRoles().getRoleName()));

In the code above:


This line of code takes the password after the database is encrypted. The purpose of splicing {bcrypt} in front is to indicate which encryption method is used for the database password. When verifying the password passed in by the front end, Security should be used Which way to verify the password.

This is the solution to this problem

created at:12-20-2021
edited at: 12-27-2021: