版本:Spring Boot 2.0 + Spring Security 5.0.3
1.spring security登录验证流程参考
https://blog.csdn.net/abcwanglinyong/article/details/80981389
爬过的坑
1.配置WebSecurityConfig的logionPage的时候,不用加上项目名称。sercurity会默认加上项目名称前缀。

但是在前端请求的时候,必须加上项目名。

2.前端登录请求对应loginProcessingUrl配置对应登录验证成功后的Controller,三条路径必须一致



3. PasswordEncoder 密码加密方式问题:There is no PasswordEncoder mapped for the id “null”,官方推荐使用
BCryptPasswordEncoder

当然,还有其他的密码验证方式,通过接口PasswordEncoder的matches方法实现可以找到

4.auth.userDetailsService(customUserService()) 方式下的 new SimpleGrantedAuthority(roles.getRname()) roles前缀问题,roles的数据库里必须存上ROLE_ 前缀, 如:(ROLE_USER,ROLE_ADMIN),因为 hasRole(“user”)时会自动加上ROLE_前缀变成 ROLE_user ,如果不加前缀一般就会出现403错误。
本文介绍了Spring Boot 2.0与Spring Security 5.0.3结合时的登录验证流程,包括登录页面配置、登录请求路径匹配及PasswordEncoder的使用。在配置WebSecurityConfig时,注意登录页URL无需包含项目名,但前端请求需加上。登录验证成功后,需确保Controller路径与loginProcessingUrl一致。使用BCryptPasswordEncoder作为密码加密方式,并在userDetailsService中,数据库中的角色需以ROLE_为前缀,以避免403错误。
&spm=1001.2101.3001.5002&articleId=85011917&d=1&t=3&u=8f858afd3a394773a2d2ad326f5b121b)
577

被折叠的 条评论
为什么被折叠?



