百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

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

nanshan 2024-10-30 02:56 12 浏览 0 评论

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

使用 Spring Security 认证、鉴权机制

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

认证(登录)流程

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

认证入口为 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:通过手机号码从数据库获取账号信息
  • 认证结果处理
  • 认证成功,调用 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 配置生效。
  • 鉴权流程及涉及的主要类如下图,

    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
  • 获取资源权限信息 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
  • 鉴权投票者 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_FULLY,IS_AUTHENTICATED_REMEMBERED,IS_AUTHENTICATED_ANONYMOUSLY 其中一个,则参与鉴权投票
  • 鉴权结果处理
  • 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” 获取源码地址。

    相关推荐

    Linux 的磁盘系统,和你了解的Windows差别很大

    我的C盘去哪了?一个系统,如果没有存储,那么也就不能称之为系统。存储性是一个完整系统的重要组成部分。例如AWS最开始的服务就是S3(用来存储数据的云服务),足以见得存储对于一个应用平台是多么的重要。...

    一文读懂 Linux 硬盘挂载:从问题到解决方案

    各位互联网大厂的后端开发伙伴们!在咱们日常工作中,操作Linux系统是常有的事儿吧。你们有没有遇到过这样的场景:新添加了一块硬盘,满心欢喜准备用来存储重要数据或者部署新的应用服务,却突然发现不知道...

    硬盘分区(硬盘分区格式)

     磁盘(硬盘)分区,可以分C、D、E等分区,大家可能都会用,会根据自已的需要确定所需的空间,但分区是如何工作的呢,内容如下。Windows中有3类:MBR分区:MasterBootRecord,也...

    parted命令工具分区介绍(particle命令)

    linux系统磁盘分区通常可以使用fdisk和parted命令,当分区大小小于2TB的时候,两种皆可以使用,当分区大于2TB的话,就需要用parted分区。以下介绍parted命令相关使用,以sdb为...

    Linux 服务器上查看磁盘类型的方法

    方法1:使用lsblk命令lsblk输出说明:TYPE列显示设备类型,如disk(物理磁盘)、part(分区)、rom(只读存储)等。NAME列显示设备名称(如sda、nvme0n1)。TR...

    Linux分区命令fdisk和parted使用介绍

    摘要:一般情况下,Linux分区都是选择fdisk工具,要求硬盘格式为MBR格式,能支持的最大分区空间为2T。但是目前在实际生产环境中使用的磁盘空间越来越大,呈TB级别增长;而常用的fdisk这个工具...

    linux 分区原理与名词解释(linux操作系统中的分区类型)

    分区的意义将磁盘分成几份,每份挂在到文件系统的那个目录在linux里的文件系统Ext2:早期的格式,不支持日志功能Ext3:ext2改良版,增加了日志功能,是最基本且最常用的使用格式了Ext4:针对e...

    linux 分区合并(linux合理分区)

    查看虚拟机当前磁盘挂载情况fdisk-l选择磁盘fdisk/dev/sda查看磁盘分区情况p重新选择分区n选择主分区p保存w创建物理卷pvcreate/dev/sda3查看物理卷信息pvdi...

    如何在 Linux 系统中永久禁用交换分区 ?

    Linux操作系统中的交换分区或交换文件充当硬盘上的临时存储区域,当物理内存(RAM)满时,系统使用该存储区域。它用于交换较少使用的内存页,这样系统就不会因为运行应用程序而耗尽物理内存。随着技术的发...

    Linux 如何知道硬盘已用多少空间、未用多少空间

    刚出社会时,去了一家公司上班,老板为了省钱,买的服务器是低配的,硬盘大小只有40G,有一次网站突然不能访问了,排查半天才知道原来服务器的硬盘空间已用完,已无可用空间。第一步是查看硬盘的使用情况,第二步...

    用Linux系统管理磁盘空间 就该这么来

    要想充分有效的管理使用Linux系统中的存储空间,用户必须要做的就是双管齐下,一边扩充空间一边限制空间。不得不说的就是很多时候磁盘空间就像水资源,需节制水流。说到要如何实现限制空间就离不开使用LVM技...

    Windows 11 磁盘怎么分区?(windows11磁盘怎么分区)

    Windows11磁盘分区技术解析与操作指南:构建高效存储体系一、磁盘分区的技术本质与系统价值磁盘分区作为存储系统的基础架构,通过逻辑划分实现数据隔离与管理优化。Windows11采用NTF...

    linux上创建多个文件分区,格式化为 ext2、ext3、ext4、XFS 文件

    以下是在Linux系统上创建多个20GB文件分区并格式化为不同文件系统的分步指南:步骤1:创建基础文件(4个20GB文件)bash#创建4个20GB稀疏文件(实际占用空间随写入量增长)ddif=/...

    救命的U盘低格哪家最强?(低格优盘)

    周二时有位童鞋留言说U盘之前做过引导盘,现在格式化不了,用各种工具都不行,而且因为U盘厂商的关系,查不到U盘主控,无法量产恢复,特来求助。小编花了点时间特意弄坏一个U盘分区,终于试出方法了,特来分享一...

    Linux 查看硬件磁盘存储大小和磁盘阵列(RAID)的组合方式

    一、查看硬件磁盘存储大小查看所有磁盘信息:#lsblk该命令会列出所有磁盘(如/dev/sda、/dev/nvme0n1)及其分区和挂载点。查看磁盘总容量:fdisk-l#或parted-...

    取消回复欢迎 发表评论: