Spring中的@EnableWebSecurity注解詳解
@EnableWebSecurity注解
首先,EnableWebSecurity注解是個(gè)組合注解,它的注解中,又使用了@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配置類(lèi),在這個(gè)配置類(lèi)中, 注入了一個(gè)非常重要的bean, bean的name為: springSecurityFilterChain
這是Spring Secuity的核心過(guò)濾器, 這是請(qǐng)求的認(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 {
}在這個(gè)注解中,激活了AuthenticationConfiguration配置類(lèi), 這個(gè)類(lèi)是來(lái)配置認(rèn)證相關(guān)的核心類(lèi), 這個(gè)類(lèi)的主要作用是,向spring容器中注入AuthenticationManagerBuilder。 這個(gè)類(lèi)使用了建造者模式, 它能構(gòu)建AuthenticationManager, 這個(gè)類(lèi)前面提過(guò),是身份認(rèn)證的入口。
總結(jié)
EnableWebSecurity注解有兩個(gè)作用:
- 加載了WebSecurityConfiguration配置類(lèi), 配置安全認(rèn)證策略。
- 加載了AuthenticationConfiguration, 配置了認(rèn)證信息。
到此這篇關(guān)于Spring中的@EnableWebSecurity注解詳解的文章就介紹到這了,更多相關(guān)@EnableWebSecurity注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring如何實(shí)現(xiàn)依賴(lài)注入DI(spring-test方式)
本文主要介紹如何實(shí)現(xiàn)spring 的依賴(lài)注入,并且淺顯的講述一下注入需要注意的事項(xiàng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
spring boot中使用RabbitMQ routing路由詳解
本篇文章主要介紹了spring boot中使用RabbitMQ routing路由詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
java客戶(hù)端Etcd官方倉(cāng)庫(kù)jetcd中KeepAlive接口實(shí)現(xiàn)
這篇文章主要為大家介紹了java客戶(hù)端Etcd官方倉(cāng)庫(kù)jetcd中KeepAlive接口實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,多多加薪2022-02-02
Java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例
這篇文章主要介紹了java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
一篇文章教你用Java使用JVM工具檢測(cè)問(wèn)題
這篇文章主要介紹了深入理解Java使用JVM工具檢測(cè)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09
Java代理模式之靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別及優(yōu)缺點(diǎn)
代理模式是一種常用的設(shè)計(jì)模式,它允許通過(guò)引入一個(gè)代理對(duì)象來(lái)控制對(duì)目標(biāo)對(duì)象的訪問(wèn),在Java中,代理模式被廣泛應(yīng)用,它可以提供額外的功能,如權(quán)限檢查、緩存、日志記錄等,本文將介紹靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別及優(yōu)缺點(diǎn),需要的朋友可以參考下2023-06-06

