Spring Security(六)—SpringSecurityFilterChain 加载流程深度解析

SpringSecurityFilterChain 作为 SpringSecurity 的核心过滤器链在整个认证授权过程中起着举足轻重的地位,每个请求到来,都会经过该过滤器链,前文 《Spring Security( 四)– 核心过滤器源码分析》 中我们分析了 SpringSecurityFilterChain 的构成,但还有很多疑问可能没有解开:


该如何设计你的 PasswordEncoder?

缘起

前端时间将一个集成了 spring-security-oauth2 的旧项目改造了一番,将 springboot 升级成了 springboot 2.0,众所周知 springboot 2.0 依赖的是 spring5,并且许多相关的依赖都发生了较大的改动,与本文相关的改动罗列如下,有兴趣的同学可以看看:Spring Security 5.0 New Features ,增强了 oauth2 集成的功能以及和一个比较有意思的改动—重构了密码编码器的实现(Password Encoding,由于大多数 PasswordEncoder 相关的算法是 hash 算法,所以本文将 PasswordEncoder 翻译成‘密码编码器’和并非‘密码加密器’)官方称之为

Modernized Password Encoding — 现代化的密码编码方式


Spring Security(五)-- 动手实现一个 IP_Login

在开始这篇文章之前,我们似乎应该思考下为什么需要搞清楚 Spring Security 的内部工作原理?按照第二篇文章中的配置,一个简单的表单认证不就达成了吗?更有甚者,为什么我们不自己写一个表单认证,用过滤器即可完成,大费周章引入 Spring Security,看起来也并没有方便多少。对的,在引入 Spring Security 之前,我们得首先想到,是什么需求让我们引入了 Spring Security,以及为什么是 Spring Security,而不是 shiro 等等其他安全框架。我的理解是有如下几点:

1 在前文的介绍中,Spring Security 支持防止 csrf 攻击,session-fixation protection,支持表单认证,basic 认证,rememberMe… 等等一些特性,有很多是开箱即用的功能,而大多特性都可以通过配置灵活的变更,这是它的强大之处。

2 Spring Security 的兄弟的项目 Spring Security SSO,OAuth2 等支持了多种协议,而这些都是基于 Spring Security 的,方便了项目的扩展。

3 SpringBoot 的支持,更加保证了 Spring Security 的开箱即用。

4 为什么需要理解其内部工作原理? 一个有自我追求的程序员都不会满足于浅尝辄止,如果一个开源技术在我们的日常工作中十分常用,那么我偏向于阅读其源码,这样可以让我们即使排查不期而至的问题,也方便日后需求扩展。

5 Spring 及其子项目的官方文档是我见过的最良心的文档!~~ 相比较于 Apache 的部分文档 ~~

这一节,为了对之前分析的 Spring Security 源码和组件有一个清晰的认识,介绍一个使用 IP 完成登录的简单 demo。


Spring Security(四)-- 核心过滤器源码分析

前面的部分,我们关注了 Spring Security 是如何完成认证工作的,但是另外一部分核心的内容:过滤器,一直没有提到,我们已经知道 Spring Security 使用了 springSecurityFillterChian 作为了安全过滤的入口,这一节主要分析一下这个过滤器链都包含了哪些关键的过滤器,并且各自的使命是什么。

4 过滤器详解

4.1 核心过滤器概述

由于过滤器链路中的过滤较多,即使是 Spring Security 的官方文档中也并未对所有的过滤器进行介绍,在之前,《Spring Security(二)–Guides》入门指南中我们配置了一个表单登录的 demo,以此为例,来看看这过程中 Spring Security 都帮我们自动配置了哪些过滤器。

1
2
3
4
5
6
7
8
9
10
11
12
Creating filter chain: o.s.s.web.util.matcher.AnyRequestMatcher@1, 
[o.s.s.web.context.SecurityContextPersistenceFilter@8851ce1,
o.s.s.web.header.HeaderWriterFilter@6a472566, o.s.s.web.csrf.CsrfFilter@61cd1c71,
o.s.s.web.authentication.logout.LogoutFilter@5e1d03d7,
o.s.s.web.authentication.UsernamePasswordAuthenticationFilter@122d6c22,
o.s.s.web.savedrequest.RequestCacheAwareFilter@5ef6fd7f,
o.s.s.web.servletapi.SecurityContextHolderAwareRequestFilter@4beaf6bd,
o.s.s.web.authentication.AnonymousAuthenticationFilter@6edcad64,
o.s.s.web.session.SessionManagementFilter@5e65afb6,
o.s.s.web.access.ExceptionTranslationFilter@5b9396d3,
o.s.s.web.access.intercept.FilterSecurityInterceptor@3c5dbdf8
]

Spring Security(二)--Guides

上一篇文章《Spring Security(一)–Architecture Overview》,我们介绍了 Spring Security 的基础架构,这一节我们通过 Spring 官方给出的一个 guides 例子,来了解 Spring Security 是如何保护我们的应用的,之后会对进行一个解读。

[TOC]

2 Spring Security Guides

2.1 引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

由于我们集成了 springboot,所以不需要显示的引入 Spring Security 文档中描述 core,config 依赖,只需要引入 spring-boot-starter-security 即可。


Spring Security(一)--Architecture Overview

一直以来我都想写一写 Spring Security 系列的文章,但是整个 Spring Security 体系强大却又繁杂。陆陆续续从最开始的 guides 接触它,到项目中看了一些源码,到最近这个月为了写一写这个系列的文章,阅读了好几遍文档,最终打算尝试一下,写一个较为完整的系列文章。

较为简单或者体量较小的技术,完全可以参考着 demo 直接上手,但系统的学习一门技术则不然。以我的认知,一般的文档大致有两种风格:Architecture First 和 Code First。前者致力于让读者先了解整体的架构,方便我们对自己的认知有一个宏观的把控,而后者以特定的 demo 配合讲解,可以让读者在解决问题的过程中顺便掌握一门技术。关注过我博客或者公众号的朋友会发现,我之前介绍技术的文章,大多数是 Code First,提出一个需求,介绍一个思路,解决一个问题,分析一下源码,大多如此。而学习一个体系的技术,我推荐 Architecture First,正如本文标题所言,这篇文章是我 Spring Security 系列的第一篇,主要是根据 Spring Security 文档选择性 ~~ 翻译 ~~ 整理而成的一个架构概览,配合自己的一些注释方便大家理解。写作本系列文章时,参考版本为 Spring Security 4.2.3.RELEASE。

[TOC]

1 核心组件

这一节主要介绍一些在 Spring Security 中常见且核心的 Java 类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。

1.1 SecurityContextHolder

SecurityContextHolder 用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限… 这些都被保存在 SecurityContextHolder 中。SecurityContextHolder 默认使用 ThreadLocal 策略来存储认证信息。看到 ThreadLocal 也就意味着,这是一种与线程绑定的策略。Spring Security 在用户登录时自动绑定认证信息到当前线程,在用户退出时,自动清除当前线程的认证信息。但这一切的前提,是你在 web 场景下使用 Spring Security,而如果是 Swing 界面,Spring 也提供了支持,SecurityContextHolder 的策略则需要被替换,鉴于我的初衷是基于 web 来介绍 Spring Security,所以这里以及后续,非 web 的相关的内容都一笔带过。

获取当前用户的信息

因为身份信息是与线程绑定的,所以可以在程序的任何地方使用静态方法获取用户信息。一个典型的获取当前登录用户的姓名的例子如下所示:

1
2
3
4
5
6
7
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (principal instanceof UserDetails) {
String username = ((UserDetails)principal).getUsername();
} else {
String username = principal.toString();
}

getAuthentication()返回了认证信息,再次 getPrincipal() 返回了身份信息,UserDetails 便是 Spring 对身份信息封装的一个接口。Authentication 和 UserDetails 的介绍在下面的小节具体讲解,本节重要的内容是介绍 SecurityContextHolder 这个容器。


Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×