我剛剛閱讀了另一個問題的答案,Spring 中 @EnableWebSecurity 的用途是什么?,但我不明白為什么我們需要添加@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..
}
}
uj5u.com熱心網友回復:
如果你沒有使用 spring-boot 而只是一個純 spring 專案,你肯定需要添加@EnableWebSecurity才能啟用 spring-security。
但是如果你使用的是spring-boot 2.0 ,你不需要自己添加,因為如果你忘記添加,spring-boot自動配置會自動為你添加。在封面下,它是由它完成的WebSecurityEnablerConfiguration,javadoc它還宣告了這種行為,如下所示:
如果有一個 WebSecurityConfigurerAdapter 型別的 bean,這將添加 @EnableWebSecurity 注釋。這將確保注釋存在默認安全自動配置,并且如果用戶添加自定義安全并忘記添加注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429138.html
上一篇:java-如何在JavaSpringBoot中使用@Component注釋在創建Bean之前使用@Value注釋獲取值
下一篇:如何在測驗中覆寫單個應用程式屬性
