1.前言

官方解释如下

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.
Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements.

https://spring.io/projects/spring-security

SpringSecurity项目继承WebSecurityConfigurerAdapter完成拦截鉴权逻辑,如下

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();}@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("USER");}
}

Spring鉴权认证思路就是利用J2EE的Filter,上面的WebSecurityConfigurerAdapter大量的configure操作,最终目的就是为了创建一个FilterChainProxy(bean's name: springSecurityFilterChain)实例。

为了分析SpringSecurity框架原理,本文带大家追溯源码,分析springSecurityFilterChain的定义初始化过程。

1.启动入口

J2EE定义了javax.servlet.ServletContainerInitializer接口,就是为了替换以前普通的web项目的web.xml功能,使用者可以使用ServletContainerInitializer和SPI完成servlet/filter/listener定义,可以看官方文档定义。

Spring Framework定义了SpringServletContainerInitializer类和WebApplicationInitializer接口,SpringBoot也定义了函数接口org.springframework.boot.web.servlet.ServletContextInitializer,两者功能类似,都是替换web.xml定义使用的,但是SpringBoot新定义类可以解决war包的冲突问题,场景如:

假如项目pay-web打包为pay-web.war,pay-web.war同时包含了SpringBoot和SpringFramework相关类,但pay-web.war只用到SpringFramework的特性,跑在Tomcat容器。

容器在加载SpringFramework的SpringServletContainerInitializer,会默认寻找类路径所有的WebApplicationInitializer实现类,这个SpringBoot的starter复用SpringFramework接口WebApplicationInitializer,实现了相关的逻辑,那么会出现问题。

所以SpringBoot单独定义了一个ServletContextInitializer类。详细可以看官方文档。

SpringBoot使用的AnnotationConfigServletWebServerApplicationContext的容器,它父类ServletWebServerApplicationContext的onRefresh方法提供了构建webServer细节,即从Spring的BeanFactory获取所有的ServletContextInitializer类。

SpringSecurity使用SecurityFilterAutoConfiguration构建了DelegatingFilterProxyRegistrationBean实例。

而我们的DelegatingFilterProxyRegistrationBean刚好就是一个ServletContextInitializer的实现类。

DelegatingFilterProxyRegistrationBean执行onStartup()方法逻辑,会注册Filter到ServletContext中,如下

上图所示,留意上面的$1,所以Filter的实现类是一个DelegatingFilterProxy匿名继承类

2.小结

  a).SpringBoot初始化SpringSecurity的@Configuration类,构建一个匿名类DelegatingFilterProxyRegistrationBean$1实例,这个匿名类继承DelegatingFilterProxy。

  b).匿名类DelegatingFilterProxyRegistrationBean$1实例(继承DelegatingFilterProxy)包裹了springSecurityFilterChain实例。

  c).匿名类DelegatingFilterProxyRegistrationBean$1实例注册Filter映射url(默认为/*)到ServletContext中。

3.springSecurityFilterChain加载

SpringSecurity通过类WebSecurityConfiguration定义了一个bean名为springSecurityFilterChain的实例,如下。

上图显示springSecurityFilterChain实质上是通过WebSecurity调用build方法完成构建的,下面是WebSecurity的performBuild详情

所以,我们的springSecurityFilterChain实例实质上是FilterChainProxy类型,FilterChainProxy有两类组成:

  a).ignoredRequests生成的DefaultSecurityFilterChain实例

  b).模板类SecurityBuilder(实质上就是HttpSecurity类)调用buid方法生成的实例。

上图所示,HttpSecurity就是上面内容"前言"例子的configure参数HttpSecurity,所以我们通过WebSecurityConfigurerAdapter调用configure方法配置HttpSecurity结果会在这里用上。

4.HttpSecurity构建

未完待续……

转载于:https://www.cnblogs.com/dylanli/p/10220724.html

SpringSecurity源码解读相关推荐

  1. Bert系列(二)——源码解读之模型主体

    本篇文章主要是解读模型主体代码modeling.py.在阅读这篇文章之前希望读者们对bert的相关理论有一定的了解,尤其是transformer的结构原理,网上的资料很多,本文内容对原理部分就不做过多 ...

  2. Bert系列(三)——源码解读之Pre-train

    https://www.jianshu.com/p/22e462f01d8c pre-train是迁移学习的基础,虽然Google已经发布了各种预训练好的模型,而且因为资源消耗巨大,自己再预训练也不现 ...

  3. linux下free源码,linux命令free源码解读:Procps free.c

    linux命令free源码解读 linux命令free源码解读:Procps free.c 作者:isayme 发布时间:September 26, 2011 分类:Linux 我们讨论的是linux ...

  4. nodeJS之eventproxy源码解读

    1.源码缩影 !(function (name, definition) { var hasDefine = typeof define === 'function', //检查上下文环境是否为AMD ...

  5. PyTorch 源码解读之即时编译篇

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 作者丨OpenMMLab 来源丨https://zhuanlan.zhihu.com/ ...

  6. Alamofire源码解读系列(九)之响应封装(Response)

    本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...

  7. Feflow 源码解读

    Feflow 源码解读 Feflow(Front-end flow)是腾讯IVWEB团队的前端工程化解决方案,致力于改善多类型项目的开发流程中的规范和非业务相关的问题,可以让开发者将绝大部分精力集中在 ...

  8. spring-session源码解读 sesion

    2019独角兽企业重金招聘Python工程师标准>>> spring-session源码解读 sesion 博客分类: java spring 摘要: session通用策略 Ses ...

  9. 前端日报-20160527-underscore 源码解读

    underscore 源码解读 API文档浏览器 JavaScript 中加号操作符细节 抛弃 jQuery,拥抱原生 JS 从 0 开始学习 GitHub 系列之「加入 GitHub」 js实现克隆 ...

  10. php service locator,Yii源码解读-服务定位器(ServiceLocator)

    SL的目的也是解耦,并且非常适合基于服务和组件的应用. Service Locator充当了一个运行时的链接器的角色,可以在运行时动态地修改一个类所要选用的服务, 而不必对类作任何的修改. 一个类可以 ...

最新文章

  1. ICML进行时 | 一文看尽获奖论文及Google、Facebook、微软、腾讯的最新科研成果
  2. Linux中设置服务自启动的三种方式
  3. java反射构造函数_【译】3. Java反射——构造函数
  4. c#geckofx文件流下载
  5. jmail 收件(转)
  6. python创建列表的语句_如何使用列表作为参数创建SELECT语句? - python
  7. 校招刷题---java选择题笔记02
  8. 【推荐】JSON在线格式化工具
  9. nginx location 匹配 多个规则_nginx配置location与rewrite规则教程
  10. Hadoop:一文详解MapReduce的工作机制
  11. 工作基本功:问题解决不了或不满意,不要重复,应该向上级反映或投诉
  12. 《车间调度及其遗传算法》学习——前言
  13. 医院信息化建设,对信息科相关工作者提出了怎样的要求?
  14. Javascript小程序 向浏览者问好(转)
  15. 尚硅谷javaWeb书城项目第一阶段 用js实现
  16. php throw,PHP的Try, throw 和 catch简单用法
  17. 常见嵌入式WEB服务器
  18. Mysql必知必会概要总结
  19. 【GD32L233C-START】8、使用内部参考电压校准adc,adc采样更准确
  20. 【Java】Java实现找图抓色

热门文章

  1. gatsbyjs 了解
  2. a better git log
  3. SurfaceView实例
  4. EF BB BF的问题
  5. ospf 几种LSA类型的总结
  6. Sharepoint Portal Server 2005?
  7. 看了扎心:39岁单身程序员入住养老院!养老院:院内平均年龄瞬间年轻了
  8. 微服务架构是啥?一个故事告诉你!
  9. 谁说不能用 Python开发企业应用?
  10. 首席架构师眼中的架构应该是怎样的?