SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解
@EnableWebSecurity注解
@EnableWebSecurity是Spring Security用于啟用Web安全的注解。
典型的用法是該注解用在某個Web安全配置類上(實現(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 ... }
該注解其實起到了如下效果 :
控制Spring Security是否使用調(diào)試模式(通過注解屬性debug指定),缺省為false,表示缺省不使用調(diào)試模式;
導(dǎo)入 WebSecurityConfiguration,用于配置Web安全過濾器FilterChainProxy;
若干個WebSecurityConfigurerAdapter作用于一個WebSecurity生成一個最終使用的web安全過濾器FilterChainProxy
如果是Servlet 環(huán)境,導(dǎo)入WebMvcSecurityConfiguration;
如果是OAuth2環(huán)境,導(dǎo)入OAuth2ClientConfiguration;
使用注解@EnableGlobalAuthentication啟用全局認證機制;
Spring Security依賴于全局認證機制,所以這里啟用全局認證機制是很自然的事。
注解@EnableGlobalAuthentication又導(dǎo)入了AuthenticationConfiguration用于全局認證機制配置;
AuthenticationConfiguration主要目的用于配置認證管理器組件AuthenticationManager。
AuthenticationManager會在運行時用于認證請求者身份。
在非Springboot的Spring Web MVC應(yīng)用中,該注解@EnableWebSecurity需要開發(fā)人員自己引入以啟用Web安全。而在基于Springboot的Spring Web MVC應(yīng)用中,開發(fā)人員沒有必要再次引用該注解,Springboot的自動配置機制WebSecurityEnablerConfiguration已經(jīng)引入了該注解,如下所示:
package org.springframework.boot.autoconfigure.security.servlet; // 省略 imports 行 @Configuration // 僅在存在 WebSecurityConfigurerAdapter bean 時該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnBean(WebSecurityConfigurerAdapter.class) // 僅在不存在 springSecurityFilterChain 時該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN) // 僅在 Servlet 環(huán)境下該注解才有可能生效 // (最終生效與否要結(jié)合其他條件綜合考慮) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @EnableWebSecurity // <====== 這里啟用了 Web 安全 public class WebSecurityEnablerConfiguration { }
WebSecurityEnablerConfiguration對注解@EnableWebSecurity的使用并沒有遵循上面所舉的典型用法的例子。實際上,一個Spring Web應(yīng)用中,WebSecurityConfigurerAdapter可能有多個 , @EnableWebSecurity可以不用在任何一個WebSecurityConfigurerAdapter上,可以用在每個WebSecurityConfigurerAdapter上,也可以只用在某一個WebSecurityConfigurerAdapter上。多處使用@EnableWebSecurity注解并不會導(dǎo)致問題,其最終運行時效果跟使用@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 }) // 啟用全局安全認證機制 @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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java EasyExcel導(dǎo)出報內(nèi)存溢出的解決辦法
使用EasyExcel進行大數(shù)據(jù)量導(dǎo)出時容易導(dǎo)致內(nèi)存溢出,特別是在導(dǎo)出百萬級別的數(shù)據(jù)時,你有遇到過這種情況嗎,以下是小編整理的解決該問題的一些常見方法,需要的朋友可以參考下2024-10-10Java基礎(chǔ)之查找文本特定內(nèi)容后進行修改
這篇文章主要介紹了Java基礎(chǔ)之查找文本特定內(nèi)容后進行修改,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例
本篇文章主要介紹了SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例,使用分頁插件的原因,簡化了sql代碼的寫法,實現(xiàn)較好的物理分頁,非常具有實用價值,需要的朋友可以參考下2018-03-03詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08淺談Spring Boot 微服務(wù)項目的推薦部署方式
這篇文章主要介紹了淺談Spring Boot 微服務(wù)項目的推薦部署方式,具有一定參考價值,需要的朋友可以了解下。2017-09-09Java8 Stream Collectors收集器使用方法解析
這篇文章主要介紹了Java8 Stream Collectors收集器使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08