@EnableWebSecurity
是Spring Security
用于启用Web
安全的注解。典型的用法是该注解用在某个Web
安全配置类上(实现了接口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
是否使用调试模式(通过注解属性debug
指定),缺省为false
,表示缺省不使用调试模式; - 导入
WebSecurityConfiguration
,用于配置Web
安全过滤器FilterChainProxy
;- 若干个
WebSecurityConfigurerAdapter
作用于一个WebSecurity
生成一个最终使用的web
安全过滤器FilterChainProxy
- 若干个
- 如果是
Servlet
环境,导入WebMvcSecurityConfiguration
; - 如果是
OAuth2
环境,导入OAuth2ClientConfiguration
; - 使用注解
@EnableGlobalAuthentication
启用全局认证机制;Spring Security
依赖于全局认证机制,所以这里启用全局认证机制是很自然的事。
注解@EnableGlobalAuthentication
又导入了AuthenticationConfiguration
用于全局认证机制配置;
AuthenticationConfiguration
主要目的用于配置认证管理器组件AuthenticationManager
。
AuthenticationManager
会在运行时用于认证请求者身份。
在非Springboot
的Spring Web MVC
应用中,该注解@EnableWebSecurity
需要开发人员自己引入以启用Web
安全。而在基于Springboot
的Spring Web MVC
应用中,开发人员没有必要再次引用该注解,Springboot
的自动配置机制WebSecurityEnablerConfiguration
已经引入了该注解,如下所示:
package org.springframework.boot.autoconfigure.security.servlet;
// 省略 imports 行
@Configuration
// 仅在存在 WebSecurityConfigurerAdapter bean 时该注解才有可能生效
// (最终生效与否要结合其他条件综合考虑)
@ConditionalOnBean(WebSecurityConfigurerAdapter.class)
// 仅在不存在 springSecurityFilterChain 时该注解才有可能生效
// (最终生效与否要结合其他条件综合考虑)
@ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN)
// 仅在 Servlet 环境下该注解才有可能生效
// (最终生效与否要结合其他条件综合考虑)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableWebSecurity // <====== 这里启用了 Web 安全
public class WebSecurityEnablerConfiguration {
}
WebSecurityEnablerConfiguration
对注解@EnableWebSecurity
的使用并没有遵循上面所举的典型用法的例子。实际上,一个Spring Web
应用中,WebSecurityConfigurerAdapter
可能有多个 , @EnableWebSecurity
可以不用在任何一个WebSecurityConfigurerAdapter
上,可以用在每个WebSecurityConfigurerAdapter
上,也可以只用在某一个WebSecurityConfigurerAdapter
上。多处使用@EnableWebSecurity
注解并不会导致问题,其最终运行时效果跟使用@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
// 导入 WebSecurityConfiguration Web安全配置,Spring Web Mvc 有关安全的配置,OAuth2 有关安全的配置
@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;
}