首页 新闻 会员 周边

在Spring Boot 2.7.0及以上版本中,如何编写安全配置类?

0
悬赏园豆:5 [已解决问题] 解决于 2022-06-22 22:10

Spring Boot在2.7.0 M2版本之后弃用了WebSecurityConfigurerAdapter类,虽然目前这个类还可以用,但是在之后不久的版本应该就会完全移除了。Spring Boot团队提供了一个从旧风格转向新风格的用例表,就是这篇https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter。

在旧版中,要配置用户认证服务,可以覆盖WebSecurityConfigurerAdapter的configure(AuthenticationManagerBuilder auth)方法,就像下面这样:

1 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
2 
3 @Override
4     protected void configure(@NotNull AuthenticationManagerBuilder auth) throws Exception {
5         auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
6     }
7 
8 }

Spring Boot团队的用例表中只给出了LDAP认证的编写方法,但是没有使用UserService的改写方法,有谁知道如何不用WebSecurityConfigurerAdapter写出用户认证服务的配置吗?

Halloworlds的主页 Halloworlds | 初学一级 | 园豆:190
提问于:2022-06-18 15:14
< >
分享
最佳答案
0

其实在新的安全配置里,已经不需要这几行代码了,只要定义好UserService和PasswordEncoder这两个Bean,Spring Security会自动使用这两个Bean。

Halloworlds | 初学一级 |园豆:190 | 2022-06-22 22:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册