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安全内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java EasyExcel导出报内存溢出的解决办法

    Java EasyExcel导出报内存溢出的解决办法

    使用EasyExcel进行大数据量导出时容易导致内存溢出,特别是在导出百万级别的数据时,你有遇到过这种情况吗,以下是小编整理的解决该问题的一些常见方法,需要的朋友可以参考下
    2024-10-10
  • Java基础之查找文本特定内容后进行修改

    Java基础之查找文本特定内容后进行修改

    这篇文章主要介绍了Java基础之查找文本特定内容后进行修改,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • Java中的Collections类的使用示例详解

    Java中的Collections类的使用示例详解

    Collections类提供了一些静态方法,这些方法能够对List集合实现常用的算法操作,这些算法是排序,填充,移位和查找等。本文将通过示例为大家详细讲讲Collections类的使用,需要的可以参考一下
    2022-12-12
  • SSM使用mybatis分页插件pagehepler实现分页示例

    SSM使用mybatis分页插件pagehepler实现分页示例

    本篇文章主要介绍了SSM使用mybatis分页插件pagehepler实现分页示例,使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,非常具有实用价值,需要的朋友可以参考下
    2018-03-03
  • 详解springboot使用异步注解@Async获取执行结果的坑

    详解springboot使用异步注解@Async获取执行结果的坑

    本文主要介绍了springboot使用异步注解@Async获取执行结果的坑,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Spring之@Lookup注解详细解析

    Spring之@Lookup注解详细解析

    这篇文章主要介绍了Spring之@Lookup注解详细解析,当采用@Autowired注解对单例bean注依赖的原型bean时,会由于单例bean只会创建一次,导致依赖的原型bean也只会注入一次,@Lookup注解可以较为优雅的解决此类问题,需要的朋友可以参考下
    2024-01-01
  • 浅谈Spring Boot 微服务项目的推荐部署方式

    浅谈Spring Boot 微服务项目的推荐部署方式

    这篇文章主要介绍了浅谈Spring Boot 微服务项目的推荐部署方式,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • java中this关键字的详细使用介绍

    java中this关键字的详细使用介绍

    大家好,本篇文章主要讲的是java中this关键字的详细使用介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • SpringBoot全局异常处理之解决404/500错误

    SpringBoot全局异常处理之解决404/500错误

    在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息,这篇文章主要给大家介绍了关于SpringBoot全局异常处理之解决404/500错误的相关资料,需要的朋友可以参考下
    2023-11-11
  • Java8 Stream Collectors收集器使用方法解析

    Java8 Stream Collectors收集器使用方法解析

    这篇文章主要介绍了Java8 Stream Collectors收集器使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08

最新评论