SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解
@EnableWebSecurity注解
@EnableWebSecurity是Spring Security用于啟用Web安全的注解。
典型的用法是該注解用在某個(gè)Web安全配置類上(實(shí)現(xiàn)了接口WebSecurityConfigurer或者繼承自WebSecurityConfigurerAdapter)。
典型的使用例子如下 :
@Configuration @EnableWebSecurity public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring() // Spring Security should completely ignore URLs starting with /resources/ .antMatchers("/resources/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/public/**").permitAll().anyRequest() .hasRole("USER").and() // Possibly more configuration ... .formLogin() // enable form based log in // set permitAll for all URLs associated with Form Login .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth // enable in memory based authentication with a user named "user" and "admin" .inMemoryAuthentication().withUser("user").password("password").roles("USER") .and().withUser("admin").password("password").roles("USER", "ADMIN"); } // Possibly more overridden methods ... }
該注解其實(shí)起到了如下效果 :
控制Spring Security是否使用調(diào)試模式(通過(guò)注解屬性debug指定),缺省為false,表示缺省不使用調(diào)試模式;
導(dǎo)入 WebSecurityConfiguration,用于配置Web安全過(guò)濾器FilterChainProxy;
若干個(gè)WebSecurityConfigurerAdapter作用于一個(gè)WebSecurity生成一個(gè)最終使用的web安全過(guò)濾器FilterChainProxy
如果是Servlet 環(huán)境,導(dǎo)入WebMvcSecurityConfiguration;
如果是OAuth2環(huán)境,導(dǎo)入OAuth2ClientConfiguration;
使用注解@EnableGlobalAuthentication啟用全局認(rèn)證機(jī)制;
Spring Security依賴于全局認(rèn)證機(jī)制,所以這里啟用全局認(rèn)證機(jī)制是很自然的事。
注解@EnableGlobalAuthentication又導(dǎo)入了AuthenticationConfiguration用于全局認(rèn)證機(jī)制配置;
AuthenticationConfiguration主要目的用于配置認(rèn)證管理器組件AuthenticationManager。
AuthenticationManager會(huì)在運(yùn)行時(shí)用于認(rèn)證請(qǐng)求者身份。
在非Springboot的Spring Web MVC應(yīng)用中,該注解@EnableWebSecurity需要開(kāi)發(fā)人員自己引入以啟用Web安全。而在基于Springboot的Spring Web MVC應(yīng)用中,開(kāi)發(fā)人員沒(méi)有必要再次引用該注解,Springboot的自動(dòng)配置機(jī)制WebSecurityEnablerConfiguration已經(jīng)引入了該注解,如下所示:
package org.springframework.boot.autoconfigure.security.servlet; // 省略 imports 行 @Configuration // 僅在存在 WebSecurityConfigurerAdapter bean 時(shí)該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnBean(WebSecurityConfigurerAdapter.class) // 僅在不存在 springSecurityFilterChain 時(shí)該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN) // 僅在 Servlet 環(huán)境下該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @EnableWebSecurity // <====== 這里啟用了 Web 安全 public class WebSecurityEnablerConfiguration { }
WebSecurityEnablerConfiguration對(duì)注解@EnableWebSecurity的使用并沒(méi)有遵循上面所舉的典型用法的例子。實(shí)際上,一個(gè)Spring Web應(yīng)用中,WebSecurityConfigurerAdapter可能有多個(gè) , @EnableWebSecurity可以不用在任何一個(gè)WebSecurityConfigurerAdapter上,可以用在每個(gè)WebSecurityConfigurerAdapter上,也可以只用在某一個(gè)WebSecurityConfigurerAdapter上。多處使用@EnableWebSecurity注解并不會(huì)導(dǎo)致問(wèn)題,其最終運(yùn)行時(shí)效果跟使用@EnableWebSecurity一次效果是一樣的。
源代碼
源代碼版本 Spring Security Config 5.1.4.RELEASE
package org.springframework.security.config.annotation.web.configuration; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; /** * * @see WebSecurityConfigurer * @see WebSecurityConfigurerAdapter * * @author Rob Winch * @since 3.2 */ @Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME) @Target(value = { java.lang.annotation.ElementType.TYPE }) @Documented // 導(dǎo)入 WebSecurityConfiguration Web安全配置,Spring Web Mvc 有關(guān)安全的配置,OAuth2 有關(guān)安全的配置 @Import({ WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class }) // 啟用全局安全認(rèn)證機(jī)制 @EnableGlobalAuthentication @Configuration public @interface EnableWebSecurity { /** * Controls debugging support for Spring Security. Default is false. * @return if true, enables debug support with Spring Security */ boolean debug() default false; }
到此這篇關(guān)于SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解的文章就介紹到這了,更多相關(guān)EnableWebSecurity注解啟用Web安全內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java EasyExcel導(dǎo)出報(bào)內(nèi)存溢出的解決辦法
使用EasyExcel進(jìn)行大數(shù)據(jù)量導(dǎo)出時(shí)容易導(dǎo)致內(nèi)存溢出,特別是在導(dǎo)出百萬(wàn)級(jí)別的數(shù)據(jù)時(shí),你有遇到過(guò)這種情況嗎,以下是小編整理的解決該問(wèn)題的一些常見(jiàn)方法,需要的朋友可以參考下2024-10-10Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改
這篇文章主要介紹了Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04SSM使用mybatis分頁(yè)插件pagehepler實(shí)現(xiàn)分頁(yè)示例
本篇文章主要介紹了SSM使用mybatis分頁(yè)插件pagehepler實(shí)現(xiàn)分頁(yè)示例,使用分頁(yè)插件的原因,簡(jiǎn)化了sql代碼的寫法,實(shí)現(xiàn)較好的物理分頁(yè),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-03-03詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08淺談Spring Boot 微服務(wù)項(xiàng)目的推薦部署方式
這篇文章主要介紹了淺談Spring Boot 微服務(wù)項(xiàng)目的推薦部署方式,具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09SpringBoot全局異常處理之解決404/500錯(cuò)誤
在搭建項(xiàng)目框架的時(shí)候用的是springboot,想統(tǒng)一處理異常,但是發(fā)現(xiàn)404的錯(cuò)誤總是捕捉不到,總是返回的是springBoot自帶的錯(cuò)誤結(jié)果信息,這篇文章主要給大家介紹了關(guān)于SpringBoot全局異常處理之解決404/500錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下2023-11-11Java8 Stream Collectors收集器使用方法解析
這篇文章主要介紹了Java8 Stream Collectors收集器使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08