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