java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

金鞍玉勒寻芳客,未信我庐别有春。这篇文章主要讲述java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id " null" 报错相关的知识,希望能为你提供帮助。
出现问题的原因:
内存用户验证时,Spring boot 2.0.1引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例,否则后台汇报错误:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
并且页面毫无响应。 
解决方法:
创建PasswordEncorder的实现类MyPasswordEncoder。
代码一:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

文章图片
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

文章图片
1 package com.mmall.demo; 2 3 import org.springframework.security.crypto.password.PasswordEncoder; 4 5 public class MyPasswordEncoder implements PasswordEncoder { 6@Override 7public String encode(CharSequence rawPassword) { 8return rawPassword.toString(); 9} 10 11@Override 12public boolean matches(CharSequence rawPassword, String encodedPassword) { 13return encodedPassword.equals(rawPassword); 14} 15 }

View Code代码二:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

文章图片

 
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

文章图片
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

文章图片
1 package com.mmall.demo; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 import org.springframework.security.config.annotation.web.builders.WebSecurity; 7 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 10 @Configuration 11 @EnableWebSecurity 12 public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 13 14@Override 15protected void configure(AuthenticationManagerBuilder auth) throws Exception { 16auth.inMemoryAuthentication(). 17passwordEncoder(new MyPasswordEncoder()). 18withUser("admin").password("123456").roles("ADMIN"); 19} 20 21@Override 22protected void configure(HttpSecurity http) throws Exception { 23http.authorizeRequests() 24.antMatchers("/").permitAll() 25.anyRequest().authenticated() 26.and() 27.logout().permitAll() 28.and() 29.formLogin(); 30http.csrf().disable(); 31} 32 33@Override 34public void configure(WebSecurity web) throws Exception { 35web.ignoring().antMatchers("/js/**","/css/**","/image/**"); 36} 37 38 39 }

View Code【java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id " null" 报错】 

    推荐阅读