Spring中的@EnableWebSecurity注解詳解
@EnableWebSecurity注解
首先,EnableWebSecurity注解是個組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class})
@EnableGlobalAuthentication
@Configuration
public @interface EnableWebSecurity {
boolean debug() default false;
}
WebSecurityConfiguration.class
首先,激活了WebSecurityConfiguration配置類,在這個配置類中, 注入了一個非常重要的bean, bean的name為: springSecurityFilterChain
這是Spring Secuity的核心過濾器, 這是請求的認(rèn)證入口。
源碼片段如下:
@Bean(
name = {"springSecurityFilterChain"}
)
public Filter springSecurityFilterChain() throws Exception {
boolean hasConfigurers = this.webSecurityConfigurers != null && !this.webSecurityConfigurers.isEmpty();
if (!hasConfigurers) {
WebSecurityConfigurerAdapter adapter = (WebSecurityConfigurerAdapter)this.objectObjectPostProcessor.postProcess(new WebSecurityConfigurerAdapter() {
});
this.webSecurity.apply(adapter);
}
return (Filter)this.webSecurity.build();
}
@Bean
@DependsOn({"springSecurityFilterChain"})
public WebInvocationPrivilegeEvaluator privilegeEvaluator() {
return this.webSecurity.getPrivilegeEvaluator();
}@EnableGlobalAuthentication
使用了EnableGlobalAuthentication 注解, 注解源碼為:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({AuthenticationConfiguration.class})
@Configuration
public @interface EnableGlobalAuthentication {
}在這個注解中,激活了AuthenticationConfiguration配置類, 這個類是來配置認(rèn)證相關(guān)的核心類, 這個類的主要作用是,向spring容器中注入AuthenticationManagerBuilder。 這個類使用了建造者模式, 它能構(gòu)建AuthenticationManager, 這個類前面提過,是身份認(rèn)證的入口。
總結(jié)
EnableWebSecurity注解有兩個作用:
- 加載了WebSecurityConfiguration配置類, 配置安全認(rèn)證策略。
- 加載了AuthenticationConfiguration, 配置了認(rèn)證信息。
到此這篇關(guān)于Spring中的@EnableWebSecurity注解詳解的文章就介紹到這了,更多相關(guān)@EnableWebSecurity注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring下Filter過濾器配置全局異常處理的詳細(xì)步驟
這篇文章主要介紹了Spring下Filter過濾器配置全局異常處理的詳細(xì)步驟,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
win10系統(tǒng)64位jdk1.8的下載與安裝教程圖解
這篇文章主要介紹了win10系統(tǒng)64位jdk1.8的下載與安裝教程圖解,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法
目前在系統(tǒng)架構(gòu)設(shè)計中使用Redis實現(xiàn)緩存,這篇文章主要介紹了使用Spring Data Redis實現(xiàn)數(shù)據(jù)緩存的方法,具有一定的參考價值,需要的朋友可以參考下2018-11-11

