SpringSecurity中的EnableWebSecurity注解启用Web安全详解

 更新时间:2023年12月25日 09:55:04   作者:安迪源文  
这篇文章主要介绍了SpringSecurity中的EnableWebSecurity注解启用Web安全详解,@EnableWebSecurity是Spring Security用于启用Web安全的注解,典型的用法是该注解用在某个Web安全配置类上,实现了接口,需要的朋友可以参考下

@EnableWebSecurity注解

@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;
}

到此这篇关于SpringSecurity中的EnableWebSecurity注解启用Web安全详解的文章就介绍到这了,更多相关EnableWebSecurity注解启用Web安全内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解全局事务注解@GlobalTransactional的识别

    详解全局事务注解@GlobalTransactional的识别

    这篇文章主要为大家介绍了详解全局事务注解@GlobalTransactional的识别源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • SpringMVC + jquery.uploadify实现上传文件功能

    SpringMVC + jquery.uploadify实现上传文件功能

    文件上传是很多项目都会使用到的功能,SpringMVC当然也提供了这个功能。不过小编不建议在项目中通过form表单来提交文件上传,这样做的局限性很大。下面这篇文章主要介绍了利用SpringMVC + jquery.uploadify实现上传文件功能的相关资料,需要的朋友可以参考下。
    2017-06-06
  • Jsoup获取全国地区数据属性值(省市县镇村)

    Jsoup获取全国地区数据属性值(省市县镇村)

    这篇文章主要介绍了Jsoup获取全国地区数据属性值(省市县镇村)的相关资料,需要的朋友可以参考下
    2015-10-10
  • 记一次在idea离线使用maven问题(推荐)

    记一次在idea离线使用maven问题(推荐)

    这篇文章主要介绍了记一次在idea离线使用maven问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Java中sharding-jdbc 绑定表的实现

    Java中sharding-jdbc 绑定表的实现

    绑定表是 ShardingSphere-JDBC 解决跨库表关联查询的核心机制,通过强制分片键和算法一致的表路由到同一物理库,实现高效本地关联,适用于大表之间的关联场景,避免了全局表的存储冗余,下面就来详细的介绍一下如何使用,感兴趣的可以了解一下
    2026-03-03
  • 详解Springboot自定义异常处理

    详解Springboot自定义异常处理

    本篇文章主要介绍了详解Springboot自定义异常处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 关于Jmeter接口测试实战-Cookies

    关于Jmeter接口测试实战-Cookies

    这篇文章主要介绍了关于Jmeter接口测试实战-Cookies问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • springcloud gateway自定义断言规则详解,以后缀结尾进行路由

    springcloud gateway自定义断言规则详解,以后缀结尾进行路由

    这篇文章主要介绍了springcloud gateway自定义断言规则详解,以后缀结尾进行路由,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • MyBatis-Plus条件构造器Wrapper应用实例

    MyBatis-Plus条件构造器Wrapper应用实例

    QueryWrapper是用于查询的Wrapper条件构造器,可以通过它来构建SELECT语句中的WHERE条件,这篇文章主要介绍了MyBatis-Plus数据表操作条件构造器Wrapper,需要的朋友可以参考下
    2023-09-09
  • java双向循环链表的实现代码

    java双向循环链表的实现代码

    这篇文章介绍了java双向循环链表的实现代码,有需要的朋友可以参考一下
    2013-09-09

最新评论