项目场景:
SpringBoot整合SpringSecurity,使用配置类进行登录名和密码配置
package com.example.helloworld;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String password = passwordEncoder.encode("123");
auth.inMemoryAuthentication().withUser("lucy").password(password).roles("admin");
}
}
问题描述
报There is no PasswordEncoder mapped for the id "null"错误
原因分析:
因为在SpringBoot和SpringSecurity整合中,使用配置类进行用户名和密码配置时,需要在spring容器里有类型为PasswordEncoder的bean,要用
解决方案:
在配置类里添加
@Bean
PasswordEncoder password(){
return new BCryptPasswordEncoder();
}