mon*_*reo 3 spring spring-security spring-boot
我刚刚阅读了另一个问题的答案@EnableWebSecurity in Spring有什么用?,但我根本不明白为什么我们需要添加@EnableWebSecurity注释。
@EnableWebSecurity即使我从配置中删除,我的应用程序仍然可以工作。
假设我们要实现基于 JWT(rest api)或仅基于登录的 mvc 应用程序。对于以下配置我缺少什么?
@Component
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService userDetailsService() {
return new MyCustomUserDetailsService();
}
@Bean
public PasswsordEncoder passwsordEncoder() {
return new BrcyptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// for the jwt authentication add jwt filter etc ..
// for the login based, override default login page, error page etc..
}
}
Run Code Online (Sandbox Code Playgroud)
Ken*_*han 11
如果你不使用 spring-boot 而只是一个纯 spring 项目,那么你肯定需要添加@EnableWebSecurity才能启用 spring-security。
但如果你使用的是 spring-boot 2.0+,你不需要自己添加它,因为如果你忘记添加,spring-boot 自动配置会自动为你添加。在幕后,它是由 完成的,WebSecurityEnablerConfiguration它还javadoc声明了此行为,如下所示:
如果存在 WebSecurityConfigurerAdapter 类型的 bean,则会添加 @EnableWebSecurity 注释。这将确保注释存在于默认安全自动配置中,并且如果用户添加自定义安全性但忘记添加注释。
| 归档时间: |
|
| 查看次数: |
2160 次 |
| 最近记录: |