Spring Security 学习(1)

版本:Spring Boot 2.0 + Spring Security 5.0.3

1.spring security登录验证流程参考

https://blog.****.net/abcwanglinyong/article/details/80981389

爬过的坑

1.配置WebSecurityConfig的logionPage的时候,不用加上项目名称。sercurity会默认加上项目名称前缀。
Spring Security 学习(1)
但是在前端请求的时候,必须加上项目名。
Spring Security 学习(1)
2.前端登录请求对应loginProcessingUrl配置对应登录验证成功后的Controller,三条路径必须一致
Spring Security 学习(1)
Spring Security 学习(1)
Spring Security 学习(1)
3. PasswordEncoder 密码加密方式问题:There is no PasswordEncoder mapped for the id “null”,官方推荐使用
BCryptPasswordEncoder
Spring Security 学习(1)
当然,还有其他的密码验证方式,通过接口PasswordEncoder的matches方法实现可以找到
Spring Security 学习(1)

4.auth.userDetailsService(customUserService()) 方式下的 new SimpleGrantedAuthority(roles.getRname()) roles前缀问题,roles的数据库里必须存上ROLE_ 前缀, 如:(ROLE_USER,ROLE_ADMIN),因为 hasRole(“user”)时会自动加上ROLE_前缀变成 ROLE_user ,如果不加前缀一般就会出现403错误。