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。

代码一:

 package com.mmall.demo;

 import org.springframework.security.crypto.password.PasswordEncoder;

 public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
return rawPassword.toString();
} @Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encodedPassword.equals(rawPassword);
}
}

代码二:

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

 package com.mmall.demo;

 import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().
passwordEncoder(new MyPasswordEncoder()).
withUser("admin").password("123456").roles("ADMIN");
} @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
} @Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**","/css/**","/image/**");
} }
上一篇:nginx的autoindex-目录浏览还有其它两个参数


下一篇:EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.