博客
关于我
Spring Security源码(七):设计模式在框架中的应用
阅读量:372 次
发布时间:2019-03-04

本文共 2314 字,大约阅读时间需要 7 分钟。

责任链模式、代理模式及其他设计模式的应用分析

在看源码的过程中发现用到的设计模式还是很多的,最近博主正好也学习了些设计模式,所以总结了下源码中所用到的设计模式,可能还有很多设计模式没找出来,有兴趣的可以一块来探讨呀。


一、责任链模式

其实接收Web请求的整个过滤器链形成的处理方式就是责任链模式。如果想看完整写的责任链模式可以参考:

org.springframework.security.web.FilterChainProxy#doFilterInternal

方法中定义了一个 VirtualFilterChain 类型的虚拟过滤器链对象,他的目的主要是

更方便的在Spring Security过滤器链中传递请求

,从它的入口处理和结束都有,很详细了。


二、代理模式

【此处是否用到了代理模式有待考究,欢迎大家来一起探讨】 FilterChainProxy 就是个代理类,它代理的是 Spring Security 中的所有过滤器链,同时它也是一个过滤器,所以可以加入到 ServletContext,使用代理目的应该是

不直接把 Spring Security 中的过滤器链直接暴露给 ServletContext

,从而达到保护的目的,还能降低程序的耦合度。


三、模板方法

org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder#doBuild

这里就使用到了模板方法

doBuild()

方法里面定义了一个算法骨架,程序按照这个算法(beforeInit()init()beforeConfigure()configure()performBuild())去执行,而算法中每个方法可以被子类去实现。


四、适配器模式

配置器中 WebSecurityConfigurerAdapter 就是个适配器类,使用适配器的好处是我们可以选择性的配置想要修改的那一部分配置,而不用覆盖其他不相关的配置。

比如它提供了三个重载的 configure 方法,我们不需要使用它的 configure(HttpSecurity http) ,于是我们自己定义一个配置类继承这个适配器,覆盖掉它的这个方法使用我们自定义的配置,而且我们一般都是这么做的。


五、建造者模式

整个建造者的架构就是个建造者模式,利用 HttpSecurityWebSecurity 对需要的一些属性进行配置,最后构建出核心过滤器 springSecurityFilterChain

比如我们使用 HttpSecurity 链式调用的方式去配置很多属性,比如配置我们需要的过滤器、自定义的过滤器等等,配置好后最终构建出所需的对象。


六、装饰者模式

来看看这个类 org.springframework.security.config.annotation.ObjectPostProcessor,里面有个 postProcess 方法,入参是 O类型,出参也是 O类型,是不是符合装饰者模式:增强原有对象,而不改变原有对象

public interface ObjectPostProcessor

扩展这个接口的类可以对原对象进行增强,例如:

org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration#springSecurityFilterChain
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder#performBuild
org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration#setFilterChainProxySecurityConfigurer

七、策略模式

在认证授权那一块用到了策略模式 + 委托模式,先来看图

https://img-blog.csdnimg.cn/20210421171853819.png?x-oss-process=image/watermark&type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MjIxNzg4,size_16,color_FFFFFF,t_70

策略+委托模式的实现方式是怎样的呢?

AuthenticationProvider

是策略接口,后面三个都是它的实现类,定义了不同的策略(这里只列出三个,中间两个是自定义的策略)

最左边的 ProviderManager 中有个 providers 属性,类型是 List<AuthenticationProvider>,它其实就是个委托类

org.springframework.security.authentication.ProviderManager#authenticate

八、委托模式

委托模式不是 GOF(Gang of Four) 23种设计模式之一。

源码示例参考策略模式。


末、系列文章

如果对以上内容感兴趣,可以继续探索更多Spring Security的设计模式应用,或者关注后续系列文章,期待你的参与!

转载地址:http://eeer.baihongyu.com/

你可能感兴趣的文章
npm发布包--所遇到的问题
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和package.json那些不为常人所知的小秘密
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm如何清空缓存并重新打包?
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>