Spring Security,没有看起来那么复杂(附源码)

2023-11-19

权限管理是每个项目必备的功能,只是各自要求的复杂程度不同,简单的项目可能一个 Filter 或 Interceptor 就解决了,复杂一点的就可能会引入安全框架,如 Shiro, Spring Security 等。 其中 Spring Security 因其涉及的流程、类过多,看起来比较复杂难懂而被诟病。但如果能捋清其中的关键环节、关键类,Spring Security 其实也没有传说中那么复杂。本文结合脚手架框架的权限管理实现(jboost-auth 模块,源码获取见文末),对 Spring Security 的认证、授权机制进行深入分析。

使用 Spring Security 认证、鉴权机制

Spring Security 主要实现了 Authentication(认证——你是谁?)、Authorization(鉴权——你能干什么?)

认证(登录)流程

Spring Security 的认证流程及涉及的主要类如下图,

SpringSecurity认证

认证入口为 AbstractAuthenticationProcessingFilter,一般实现有 UsernamePasswordAuthenticationFilter

  1. filter 解析请求参数,将客户端提交的用户名、密码等封装为 Authentication,Authentication 一般实现有 UsernamePasswordAuthenticationToken
  2. filter 调用 AuthenticationManager 的 authenticate() 方法对 Authentication 进行认证,AuthenticationManager 的默认实现是 ProviderManager
  3. ProviderManager 认证时,委托给一个 AuthenticationProvider 列表,调用列表中 AuthenticationProvider 的 authenticate() 方法来进行认证,只要有一个通过,则认证成功,否则抛出 AuthenticationException 异常(AuthenticationProvider 还有一个 supports() 方法,用来判断该 Provider 是否对当前类型的 Authentication 进行认证)
  4. 认证完成后,filter 通过 AuthenticationSuccessHandler(成功时) 或 AuthenticationFailureHandler(失败时)来对认证结果进行处理,如返回 token 或 认证错误提示

认证涉及的关键类

  1. 登录认证入口 UsernamePasswordAuthenticationFilter

项目中 RestAuthenticationFilter 继承了 UsernamePasswordAuthenticationFilter, UsernamePasswordAuthenticationFilter 将客户端提交的参数封装为 UsernamePasswordAuthenticationToken,供 AuthenticationManager 进行认证。

RestAuthenticationFilter 覆写了 UsernamePasswordAuthenticationFilter 的 attemptAuthentication(request,response) 方法逻辑,根据 loginType 的值来将登录参数封装到认证信息 Authentication 中,(loginType 为 USER 时为 UsernameAuthenticationToken, loginType 为 Phone 时为 PhoneAuthenticationToken),供下游 AuthenticationManager 进行认证。

  1. 认证信息 Authentication

使用 Authentication 的实现来保存认证信息,一般为 UsernamePasswordAuthenticationToken,包括

  • principal:身份主体,通常是用户名或手机号
  • credentials:身份凭证,通常是密码或手机验证码
  • authorities:授权信息,通常是角色 Role
  • isAuthenticated:认证状态,表示是否已认证

本项目中的 Authentication 实现:

  • UsernameAuthenticationToken: 使用用户名登录时封装的 Authentication

    • principal => username
    • credentials => password
    • 扩展了两个属性: uuid, code,用来验证图形验证码
  • PhoneAuthenticationToken: 使用手机验证码登录时封装的 Authentication

    • principal => phone(手机号)
    • credentials => code(验证码)

两者都继承了 UsernamePasswordAuthenticationToken。

  1. 认证管理器 AuthenticationManager

认证管理器接口 AuthenticationManager,包含一个 authenticate(authentication) 方法。 ProviderManager 是 AuthenticationManager 的实现,管理一个 AuthenticationProvider(具体认证逻辑提供者)列表。在其 authenticate(authentication ) 方法中,对 AuthenticationProvider 列表中每一个 AuthenticationProvider,调用其 supports(Class<?> authentication) 方法来判断是否采用该 Provider 来对 Authentication 进行认证,如果适用则调用 AuthenticationProvider 的 authenticate(authentication) 来完成认证,只要其中一个完成认证,则返回。

  1. 认证提供者 AuthenticationProvider

由3可知认证的真正逻辑由 AuthenticationProvider 提供,本项目的认证逻辑提供者包括

  • UsernameAuthenticationProvider: 支持对 UsernameAuthenticationToken 类型的认证信息进行认证。同时使用 PasswordRetryUserDetailsChecker 来对密码错误次数超过5次的用户,在10分钟内限制其登录操作
  • PhoneAuthenticationProvider: 支持对 PhoneAuthenticationToken 类型的认证信息进行认证

两者都继承了 DaoAuthenticationProvider —— 通过 UserDetailsService 的 loadUserByUsername(String username) 获取保存的用户信息 UserDetails,再与客户端提交的认证信息 Authentication 进行比较(如与 UsernameAuthenticationToken 的密码进行比对),来完成认证。

  1. 用户信息获取 UserDetailsService

UserDetailsService 提供 loadUserByUsername(username) 方法,可获取已保存的用户信息(如保存在数据库中的用户账号信息)。

本项目的 UserDetailsService 实现包括

  • UsernameUserDetailsService:通过用户名从数据库获取账号信息
  • PhoneUserDetailsService:通过手机号码从数据库获取账号信息
  1. 认证结果处理

认证成功,调用 AuthenticationSuccessHandler 的 onAuthenticationSuccess(request, response, authentication) 方法,在 SecurityConfiguration 中注入 RestAuthenticationFilter 时进行了设置。 本项目中认证成功后,生成 jwt token返回客户端。

认证失败(账号校验失败或过程中抛出异常),调用 AuthenticationFailureHandler 的 onAuthenticationFailure(request, response, exception) 方法,在 SecurityConfiguration 中注入 RestAuthenticationFilter 时进行了设置,返回错误信息。

以上关键类及其关联基本都在 SecurityConfiguration 进行配置。

  1. 工具类

SecurityContextHolder 是 SecurityContext 的容器,默认使用 ThreadLocal 存储,使得在相同线程的方法中都可访问到 SecurityContext。 SecurityContext 主要是存储应用的 principal 信息,在 Spring Security 中用 Authentication 来表示。在 AbstractAuthenticationProcessingFilter 中,认证成功后,调用 successfulAuthentication() 方法使用 SecurityContextHolder 来保存 Authentication,并调用 AuthenticationSuccessHandler 来完成后续工作(比如返回token等)。

使用 SecurityContextHolder 来获取用户信息示例:

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
    String username = ((UserDetails)principal).getUsername();
} else {
    String username = principal.toString();
}

鉴权流程

Spring Security 的鉴权(授权)有两种实现机制:

  • FilterSecurityInterceptor:通过 Filter 对 HTTP 资源的访问进行鉴权
  • MethodSecurityInterceptor:通过 AOP 对方法的调用进行鉴权。在 GlobalMethodSecurityConfiguration 中注入, 需要在配置类上添加注解 @EnableGlobalMethodSecurity(prePostEnabled = true) 使 GlobalMethodSecurityConfiguration 配置生效。

鉴权流程及涉及的主要类如下图,

springsecurity鉴权

  1. 登录完成后,一般返回 token 供下次调用时携带进行身份认证,生成 Authentication
  2. FilterSecurityInterceptor 拦截器通过 FilterInvocationSecurityMetadataSource 获取访问当前资源需要的权限
  3. FilterSecurityInterceptor 调用鉴权管理器 AccessDecisionManager 的 decide 方法进行鉴权
  4. AccessDecisionManager 通过 AccessDecisionVoter 列表的鉴权投票,确定是否通过鉴权,如果不通过则抛出 AccessDeniedException 异常
  5. MethodSecurityInterceptor 流程与 FilterSecurityInterceptor 类似

鉴权涉及的关键类

  1. 认证信息提取 RestAuthorizationFilter

对于前后端分离项目,登录完成后,接下来我们一般通过登录时返回的 token 来访问接口。

在鉴权开始前,我们需要将 token 进行验证,然后生成认证信息 Authentication 交给下游进行鉴权(授权)。

本项目 RestAuthorizationFilter 将客户端上报的 jwt token 进行解析,得到 UserDetails, 并对 token 进行有效性校验,并生成 Authentication(UsernamePasswordAuthenticationToken),通过 SecurityContextHolder 存入 SecurityContext 中供下游使用。

  1. 鉴权入口 AbstractSecurityInterceptor

三个实现:

  • FilterSecurityInterceptor:基于 Filter 的鉴权实现,作用于 Http 接口层级。FilterSecurityInterceptor 从 SecurityMetadataSource 的实现 DefaultFilterInvocationSecurityMetadataSource 获取要访问资源所需要的权限 Collection ,然后调用 AccessDecisionManager 进行授权决策投票,若投票通过,则允许访问资源,否则将禁止访问。
  • MethodSecurityInterceptor:基于 AOP 的鉴权实现,作用于方法层级。
  • AspectJMethodSecurityInterceptor:用来支持 AspectJ JointPoint 的 MethodSecurityInterceptor
  1. 获取资源权限信息 SecurityMetadataSource

SecurityMetadataSource 读取访问资源所需的权限信息,读取的内容,就是我们配置的访问规则,如我们在配置类中配置的访问规则:

@Override
protected void configure(HttpSecurity http) throws Exception{
    http.authorizeRequests()
        .antMatchers(excludes).anonymous()
        .antMatchers("/api1").hasAuthority("permission1")
        .antMatchers("/api2").hasAuthority("permission2")
        ...
}

我们可以自定义一个 SecurityMetadataSource 来从数据库或其它存储中获取资源权限规则信息。

  1. 鉴权管理器 AccessDecisionManager

AccessDecisionManager 接口的 decide(authentication, object, configAttributes) 方法对本次请求进行鉴权,其中

  • authentication:本次请求的认证信息,包含 authority(如角色) 信息
  • object:当前被调用的被保护对象,如接口
  • configAttributes:与被保护对象关联的配置属性,表示要访问被保护对象需要满足的条件,如角色

AccessDecisionManager 接口的实现者鉴权时,最终是通过调用其内部 List<AccessDecisionVoter<?>> 列表中每一个元素的 vote(authentication, object, attributes) 方法来进行的,根据决策的不同分为如下三种实现

  • AffirmativeBased:一票通过权策略。只要有一个 AccessDecisionVoter 通过(AccessDecisionVoter.vote 返回 AccessDecisionVoter. ACCESS_GRANTED),则鉴权通过。为默认实现
  • ConsensusBased:少数服从多数策略。多数 AccessDecisionVoter 通过,则鉴权通过,如果赞成票与反对票相等,则根据变量 allowIfEqualGrantedDeniedDecisions 的值来决定,该值默认为 true
  • UnanimousBased:全票通过策略。所有 AccessDecisionVoter 通过或弃权(返回 AccessDecisionVoter. ACCESS_ABSTAIN),无一反对则通过,只要有一个反对就拒绝;如果全部弃权,则根据变量 allowIfAllAbstainDecisions 的值来决定,该值默认为 false
  1. 鉴权投票者 AccessDecisionVoter

与 AuthenticationProvider 类似,AccessDecisionVoter 也包含 supports(attribute) 方法(是否采用该 Voter 来对请求进行鉴权投票) 与 vote (authentication, object, attributes) 方法(具体的鉴权投票逻辑)

FilterSecurityInterceptor 的 AccessDecisionManager 的投票者列表(AbstractInterceptUrlConfigurer.createFilterSecurityInterceptor() 中设置)包括:

  • WebExpressionVoter:验证 Authentication 的 authenticated。

MethodSecurityInterceptor 的 AccessDecisionManager 的投票者列表(GlobalMethodSecurityConfiguration.accessDecisionManager() 中设置)包括:

  • PreInvocationAuthorizationAdviceVoter: 如果 @EnableGlobalMethodSecurity 注解开启了 prePostEnabled,则添加该 Voter,对使用了 @PreAuthorize 注解的方法进行鉴权投票
  • Jsr250Voter:如果 @EnableGlobalMethodSecurity 注解开启了 jsr250Enabled,则添加该 Voter,对 @Secured 注解的方法进行鉴权投票
  • RoleVoter:总是添加, 如果 ConfigAttribute.getAttribute()ROLE_ 开头,则参与鉴权投票
  • AuthenticatedVoter:总是添加,如果 ConfigAttribute.getAttribute() 值为 IS_AUTHENTICATED_FULLYIS_AUTHENTICATED_REMEMBEREDIS_AUTHENTICATED_ANONYMOUSLY 其中一个,则参与鉴权投票
  1. 鉴权结果处理

ExceptionTranslationFilter 异常处理 Filter, 对认证鉴权过程中抛出的异常进行处理,包括:

  • authenticationEntryPoint: 对过滤器链中抛出 AuthenticationException 或 AccessDeniedException 但 Authentication 为 AnonymousAuthenticationToken 的情况进行处理。如果 token 校验失败,如 token 错误或过期,则通过 ExceptionTranslationFilter 的 AuthenticationEntryPoint 进行处理,本项目使用 RestAuthenticationEntryPoint 来返回统一格式的错误信息
  • accessDeniedHandler: 对过滤器链中抛出 AccessDeniedException 但 Authentication 不为 AnonymousAuthenticationToken 的情况进行处理,本项目使用 RestAccessDeniedHandler 来返回统一格式的错误信息

如果是 MethodSecurityInterceptor 鉴权时抛出 AccessDeniedException,并且通过 @RestControllerAdvice 提供了统一异常处理,则将由统一异常处理类处理,因为 MethodSecurityInterceptor 是 AOP 机制,可由 @RestControllerAdvice 捕获。

本项目中, RestAuthorizationFilter 在 Filter 链中位于 ExceptionTranslationFilter 的前面,所以其中抛出的异常也不能被 ExceptionTranslationFilter 捕获, 由 cn.jboost.base.starter.web.ExceptionHandlerFilter 捕获处理。

也可以将 RestAuthorizationFilter 放入 ExceptionTranslationFilter 之后,但在 RestAuthorizationFilter 中需要对 SecurityContextHolder.getContext().getAuthentication() 进行 AnonymousAuthenticationToken 的判断,因为 AnonymousAuthenticationFilter 位于 ExceptionTranslationFilter 前面,会对 Authentication 为空的请求生成一个 AnonymousAuthenticationToken,放入 SecurityContext 中。

总结

安全框架一般包括认证与授权两部分,认证解决你是谁的问题,即确定你是否有合法的访问身份,授权解决你是否有权限访问对应资源的问题。Spring Security 使用 Filter 来实现认证,使用 Filter(接口层级) + AOP(方法层级)的方式来实现授权。本文相对偏理论,但也结合了脚手架中的实现,对照查看,应该更易理解。

本文基于 Spring Boot 脚手架中的权限管理模块编写,该脚手架提供了前后端分离的权限管理实现,效果如下图,可关注作者公众号 “半路雨歌”,回复 “jboost” 获取源码地址。

jboost-admin-login jboost-admin-main


[转载请注明出处] 作者:雨歌,可以关注下作者公众号:半路雨歌 qrcode

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security,没有看起来那么复杂(附源码) 的相关文章

随机推荐

  • Java学习之FileInputStream与FileReader的区别

    我们对于文件中数据的操作无非就有两种 读出与写入 对于Java来说 对数据的操作有字节流 FileOutputStream FileInputStream 与字符流 FileWriter FileReader 这两种方法其实差不多 都是从文
  • 基于人脸的常见表情识别(1)——深度学习基础知识

    基于人脸的常见表情识别 1 深度学习基础知识 神经网络 1 感知机 2 多层感知机与反向传播 卷积神经网络 1 全连接神经网络的2大缺陷 2 卷积神经网络的崛起 卷积神经网络的基本网络层 1 什么是卷积 2 填充 Padding 3 步长
  • regexp_split_to_table,regexp_split_to_array,array,unnest 使用

    实例1 select regexp split to table hello world s select regexp split to array hello world s postgres select regexp split t
  • 2019年秋招多益前端笔试

    2019年秋招多益前端笔试
  • C++ Primer Plus 第一章

    C 简介 C 是在C语言基础上开发的一种集 面向对象编程 泛型编程和传统的过程化编程于一体的编程语言 是C语言的超集 C 面向对象的特性带来了全新的编程方法 这种方法是为应对复杂程度不断提高的现代编程任务而设计的 C 融合了3种不同的编程方
  • uniapp 用 uView 组件库中的u-picker 实现地区的 省-市-区 三级联动

    组件的引入就不多赘述了 直接看使用方法 地址我是引入的json文件数据结构大概是这个样子 例1 例2 1 先做个按钮做弹窗显示
  • linux-malloc底层实现原理

    本文大致讲解一下linux下malloc的底层实现原理 首先malloc肯定是从堆中分配内存 而堆又在用户空间中占据什么位置 通过下面这张图可以看出来 很明显是32位系统 寻址空间是4G linux系统下0 3G是用户模式 3 4G是内核模
  • python itertools模块chain函数功能详解 itertools.chain(*iterable)

    Itertools模块 itertools提供了高效快捷的用于操作迭代对象的函数 通过使用这个模块 可以简化代码 Itertools chain语法 Itertools chain iterables 代表接受可变的参数 iterables
  • 数字图像去噪典型算法及matlab实现

    转自 http gaochaojs blog 51cto com 812546 243961 图像去噪是数字图像处理中的重要环节和步骤 去噪效果的好坏直接影响到后续的图像处理工作如图像分割 边缘检测等 图像信号在产生 传输过程中都可能会受到
  • MAC下如何删除idea

    MAC下如何删除idea 进入访达 点击应用程序列表 找到idea 右键或者拖动到废纸篓 打开终端 进入自己用户名下的Library目录 一般刚打开就在自己用户名下 cd Users 你的用户名 Library 使用cd命令分别进入Logs
  • BUUCTF-EasySQL

    这是一道蛮基础的sql注入的题 打开题目 一个具有黑客色彩的登陆页面 emm 没有注册窗口 在用户名或密码处加个单引号发现报错 判断是字符型注入 通过万能密码登陆注入进去 username admin or 1 1 password 拿到f
  • 出现org.apache.http.NoHttpResponseException

    摘要 spring resttemplate使用httpclient4 4 连接池 出现org apache http NoHttpResponseException 10 0 0 25 8080 failed to respond异常的解
  • YOLOV5加速之TensorRT篇

    之前写过一篇Android版本yolov5s的博客 最近工作搞了下TensorRT 也遇到几个坑不过最终还是实现了 做一下小小的分享 这里直接上github上大牛的连接 我的代码是在他的基础上改的 里面有很多模型的加速直接看里面的yolov
  • 如何利用JS生成二维码

    问题来源 最近在做一个项目 有这么个需求 通过生成二维码来实现网页的分享 问题分析 脑海的第一反应 当然是用js来实现 自己手写 当然不是 解决方案 使用 QRCode js QRCode js 是一个用于生成二维码的 JavaScript
  • Unity 游戏背景音乐突然消失,AudioSource却还在游戏场景中(更新中)

    查看AudioSource isVirtual 是否为True 如果是则音效被系统剔除 音效被系统剔除原因 1 音效某一时刻播放的次数太多
  • BrokenPipeError: [Errno 32] Broken pipe

    BrokenPipeError Errno 32 Broken pipe 前言 今天在训练yolov5 6 1版本 突然出现BrokenPipeError Errno 32 Broken pipe错误 一 运行命令python train
  • Qt5.9.1安装包下载

    一 下载地址 附上百度网盘下载链接链接 https pan baidu com s 1wwvHe5kHwSg68hy1YM6Lhg 提取码 a999 二 安装步骤 选中qt opensource windows x86 5 9 1 exe右
  • xss-工具-Beef-Xss安装以及使用

    Beef Xss工具的简介 KaliLinux官网对这工具的介绍地址 https www kali org tools beef xss GitHub地址 https github com beefproject beef Beef Xss
  • 如何解决stata数据管理器中变量变红的问题

    目标 解决open变量变红的问题 网上说可以通过以下代码解决 实际上是缘木求鱼 encode encode 红色数字的变量名 gen 新产生的变量名 自己尝试用这个代码之后 发现对应变量不是红色了 但变成了蓝色 我开始以为问题已经解决了 但
  • Spring Security,没有看起来那么复杂(附源码)

    权限管理是每个项目必备的功能 只是各自要求的复杂程度不同 简单的项目可能一个 Filter 或 Interceptor 就解决了 复杂一点的就可能会引入安全框架 如 Shiro Spring Security 等 其中 Spring Sec