在学习SpringBoot整合spring security时,原定制的认证代码为
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//super.configure(auth);
auth.inMemoryAuthentication()
.withUser("zhangsan").password("123456").roles("VIP1","VIP2")
.and()
.withUser("lisi").password("123456").roles("VIP2","VIP3")
.and()
.withUser("wangwu").password("123456").roles("VIP1","VIP3");
}
登录用户时发现前端登录页面报错500
查看控制台发现:
异常信息大概是说没有将密码加密
经过一番查证之后发现从spring security 5.x开始(springboot2.x),需要使用密码编码器
加密方法:
* passwordEncoder(参数值如下)
* 方法1 new BCryptPasswordEncoder()
* 方法2 new Pbkdf2PasswordEncoder()
* 方法3 new SCryptPasswordEncoder()
* 或实现passwordEncoder接口
更改认证代码为方法1
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//super.configure(auth);
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")
.and()
.withUser("lisi").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")
.and()
.withUser("wangwu").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");
}
再重新登录,发现登录成功!!!