Spring中的@EnableWebSecurity注解详解
@EnableWebSecurity注解
首先,EnableWebSecurity注解是个组合注解,它的注解中,又使用了@EnableGlobalAuthentication注解:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class}) @EnableGlobalAuthentication @Configuration public @interface EnableWebSecurity { boolean debug() default false; }
WebSecurityConfiguration.class
首先,激活了WebSecurityConfiguration配置类,在这个配置类中, 注入了一个非常重要的bean, bean的name为: springSecurityFilterChain
这是Spring Secuity的核心过滤器, 这是请求的认证入口。
源码片段如下:
@Bean( name = {"springSecurityFilterChain"} ) public Filter springSecurityFilterChain() throws Exception { boolean hasConfigurers = this.webSecurityConfigurers != null && !this.webSecurityConfigurers.isEmpty(); if (!hasConfigurers) { WebSecurityConfigurerAdapter adapter = (WebSecurityConfigurerAdapter)this.objectObjectPostProcessor.postProcess(new WebSecurityConfigurerAdapter() { }); this.webSecurity.apply(adapter); } return (Filter)this.webSecurity.build(); } @Bean @DependsOn({"springSecurityFilterChain"}) public WebInvocationPrivilegeEvaluator privilegeEvaluator() { return this.webSecurity.getPrivilegeEvaluator(); }
@EnableGlobalAuthentication
使用了EnableGlobalAuthentication 注解, 注解源码为:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({AuthenticationConfiguration.class}) @Configuration public @interface EnableGlobalAuthentication { }
在这个注解中,激活了AuthenticationConfiguration配置类, 这个类是来配置认证相关的核心类, 这个类的主要作用是,向spring容器中注入AuthenticationManagerBuilder。 这个类使用了建造者模式, 它能构建AuthenticationManager, 这个类前面提过,是身份认证的入口。
总结
EnableWebSecurity注解有两个作用:
- 加载了WebSecurityConfiguration配置类, 配置安全认证策略。
- 加载了AuthenticationConfiguration, 配置了认证信息。
到此这篇关于Spring中的@EnableWebSecurity注解详解的文章就介绍到这了,更多相关@EnableWebSecurity注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring如何实现依赖注入DI(spring-test方式)
本文主要介绍如何实现spring 的依赖注入,并且浅显的讲述一下注入需要注意的事项。如有错误或未考虑完全的地方,望不吝赐教2022-03-03spring boot中使用RabbitMQ routing路由详解
本篇文章主要介绍了spring boot中使用RabbitMQ routing路由详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-03-03java客户端Etcd官方仓库jetcd中KeepAlive接口实现
这篇文章主要为大家介绍了java客户端Etcd官方仓库jetcd中KeepAlive接口实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,多多加薪2022-02-02
最新评论