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啟用全局認(rèn)證機制;
Spring Security依賴于全局認(rèn)證機制,所以這里啟用全局認(rèn)證機制是很自然的事。
注解@EnableGlobalAuthentication又導(dǎo)入了AuthenticationConfiguration用于全局認(rèn)證機制配置;
AuthenticationConfiguration主要目的用于配置認(rèn)證管理器組件AuthenticationManager。
AuthenticationManager會在運行時用于認(rèn)證請求者身份。
在非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 })
// 啟用全局安全認(rèn)證機制
@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)文章
詳解全局事務(wù)注解@GlobalTransactional的識別
這篇文章主要為大家介紹了詳解全局事務(wù)注解@GlobalTransactional的識別源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
SpringMVC + jquery.uploadify實現(xiàn)上傳文件功能
文件上傳是很多項目都會使用到的功能,SpringMVC當(dāng)然也提供了這個功能。不過小編不建議在項目中通過form表單來提交文件上傳,這樣做的局限性很大。下面這篇文章主要介紹了利用SpringMVC + jquery.uploadify實現(xiàn)上傳文件功能的相關(guān)資料,需要的朋友可以參考下。2017-06-06
Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)
這篇文章主要介紹了Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)的相關(guān)資料,需要的朋友可以參考下2015-10-10
Java中sharding-jdbc 綁定表的實現(xiàn)
綁定表是 ShardingSphere-JDBC 解決跨庫表關(guān)聯(lián)查詢的核心機制,通過強制分片鍵和算法一致的表路由到同一物理庫,實現(xiàn)高效本地關(guān)聯(lián),適用于大表之間的關(guān)聯(lián)場景,避免了全局表的存儲冗余,下面就來詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2026-03-03
關(guān)于Jmeter接口測試實戰(zhàn)-Cookies
這篇文章主要介紹了關(guān)于Jmeter接口測試實戰(zhàn)-Cookies問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進行路由
這篇文章主要介紹了springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進行路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實例
QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過它來構(gòu)建SELECT語句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下2023-09-09

