前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Security 里的filer们

Spring Security 里的filer们

作者头像
dhyuan
发布2023-01-11 15:30:47
3080
发布2023-01-11 15:30:47
举报
文章被收录于专栏:响应式编程响应式编程

这段文字主要源于对 https://docs.spring.io/spring-security/reference/servlet/architecture.html 的学习和理解,其实就是对下图的理解。

1)使用 Spring Security

要使用Spring Security,如果是在Spring Boot环境,那么只需要导入security的starter,Spring Boot就会自动做下面的事。 Creates a servlet Filter as a bean named springSecurityFilterChain. This bean is responsible for all the security (protecting the application URLs, validating submitted username and passwords, redirecting to the log in form, and so on) within your application. Registers the Filter with a bean named springSecurityFilterChain with the Servlet container for every request. Creates a UserDetailsService bean with a username of user and a randomly generated password that is logged to the console.

不使用Spring Boot的情况下,就需要自己在web.xml文件中定义springSecurityFilterChain。

代码语言:javascript
复制

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

2)入口 DelegatingFilterProxy

下面通过源码简单看下DelegatingFilterProxy实例化的过程。 Tomcat启动时会在 web 容器中初始化 DelegatingFilterProxy 实例。

DelegatingFilterProxy 本身既是一个Filter也是一个ServletContextAware的实例。Spring 使用 ContextLoaderListener 来加载spring的bean。org.springframework.web.context.support.GenericWebApplicationContext 则是servlet context和spring context真正交汇的地方。 从下面DelegatingFilterProxy实现的接口就可以感知到 DelegatingFilterProxy 最关键的作用就是作为Servlet Container 和 Spring Context的桥梁。 因为Spring要等web context初始化完成才能初始化自己的context,所以如果在spring中定义的filter beans就可以延迟初始化。通过延迟初始化就解决了Filter必须定义在Servlet Container中的问题。Spring很巧妙的通过FilterChain接口把这些filter beans串在一起。

代码语言:javascript
复制
public class DelegatingFilterProxy extends GenericFilterBean

而 ServletContextAware 是Spring的一个接口。

代码语言:javascript
复制
public abstract class GenericFilterBean implements Filter, BeanNameAware, EnvironmentAware,
    EnvironmentCapable, ServletContextAware, InitializingBean, DisposableBean

从DelegatingFilterProxy的两个关键fields:WebApplicationContext webApplicationContext 和 Filter delegate,也能知道DelegatingFilterProxy桥梁作用。

上图中的delegate是FilterChainProxy的实例。

3) FilterChainProxy 包装了 filers

下图中filterChian包含的filters是不做任何特殊配置时的16个filters。FilerChainProxy就是通过调用它拥有的filters起到了对request做filter处理的作用,这就是称它为代理的原因.

4)DelegatingFilterProxy得到springSecurityFilterChain

上面提到 DelegatingFilterProxy 对filter延迟初始化的作用。所以对delegate的赋值只发生处理第一个http request时。被命名为springSecurityFilterChain的FilterChainProxy会从spring context中被找出来并设置到DelegatingFilterProxy的delegate field。

至此,应该对文首的第一个图能说出点儿什么了吧… …


Reference: [1]: https://www.baeldung.com/spring-web-contexts [2]: https://docs.spring.io/spring-security/reference/servlet/architecture.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-11-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 响应式编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1)使用 Spring Security
  • 2)入口 DelegatingFilterProxy
  • 3) FilterChainProxy 包装了 filers
  • 4)DelegatingFilterProxy得到springSecurityFilterChain
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档