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

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

nanshan 2024-10-30 02:56 10 浏览 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” 获取源码地址。

    相关推荐

    Let’s Encrypt免费搭建HTTPS网站

    HTTPS(全称:HyperTextTransferProtocoloverSecureSocketLayer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入...

    使用Nginx配置TCP负载均衡(nginx tcp负载)

    假设Kubernetes集群已经配置好,我们将基于CentOS为Nginx创建一个虚拟机。以下是实验种设置的详细信息:Nginx(CenOS8Minimal)-192.168.1.50Kube...

    Nginx负载均衡及支持HTTPS与申请免费SSL证书

    背景有两台minio文件服务器已做好集群配置,一台是192.168.56.41:9000;另一台是192.168.56.42:9000。应用程序通过Nginx负载均衡调用这两台minio服务,减轻单点...

    HTTPS配置实战(https配置文件)

    原因现在网站使用HTTPS是规范操作之一,前些日子买了腾讯云服务,同时申请了域名http://www.asap2me.top/,目前该域名只支持HTTP,想升级为HTTPS。关于HTTPS的链接过程大...

    只有IP地址没有域名实现HTTPS访问方法

    一般来说,要实现HTTPS,得有个注册好的域名才行。但有时候呢,咱只有服务器的IP地址,没注册域名,这种特殊情况下,也能照样实现HTTPS安全访问,按下面这些步骤来就行:第一步,先确认公网...

    超详解:HTTPS及配置Django+HTTPS开发环境

    众所周知HTTP协议是以TCP协议为基石诞生的一个用于传输Web内容的一个网络协议,在“网络分层模型”中属于“应用层协议”的一种。在这里我们并不研究该协议标准本身,而是从安全角度去探究使用该协议传输数...

    Godaddy购买SSL之后Nginx配置流程以及各种错误的解决

    完整流程:参考地址:https://sg.godaddy.com/zh/help/nginx-generate-csrs-certificate-signing-requests-3601生成NGI...

    Nginx从安装到高可用,一篇搞定(nginx安装与配置详解)

    一、Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yuminstallgc...

    阿里云免费证书申请,配置安装,使用tomcat,支持http/https访问

    参数说明商品类型默认已选择云盾证书服务(无需修改)。云盾证书服务类型SSL证书服务的类型。默认已选择云盾SSL证书(无需修改),表示付费版SSL证书。如果您需要免费领取或付费扩容DV单域名证书【免费试...

    你试过两步实现Nginx的规范配置吗?极速生成Nginx配置小工具

    NGINX是一款轻量级的Web服务器,最强大的功能之一是能够有效地提供HTML和媒体文件等静态内容。NGINX使用异步事件驱动模型,在负载下提供可预测的性能。是当下最受欢迎的高性能的Web...

    从零开始搭建HTTPS服务(搭建https网站)

    搭建HTTPS服务的最初目的是为了开发微信小程序,因为wx.request只允许发起HTTPS请求,并且还必须和指定的域名进行网络通信。要从零开始搭建一个HTTPS的服务需要下面4...

    群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问

    安全第一步,群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问【新手导向】NAS本质还是一个可以随时随地访问的个人数据存储中心,我们在外网访问的时候,特别是在公网IP下,其实会面临着很多安...

    让网站快速升级HTTPS协议提高安全性

    为什么用HTTPS网络安全越来越受到重视,很多互联网服务网站,都已经升级改造为https协议。https协议下数据包是ssl/tcl加密的,而http包是明文传输。如果请求一旦被拦截,数据就会泄露产生...

    用Https方式访问Harbor-1.9版本(https访问流程)

    我上周在头条号写过一篇原创文章《Docker-Harbor&Docker-kitematic史上最详细双系统配置手册》,这篇算是它的姊妹篇吧。这篇文章也将用到我在头条写的另一篇原创文章的...

    如何启用 HTTPS 并配置免费的 SSL 证书

    在Linux服务器上启用HTTPS并配置免费的SSL证书(以Let'sEncrypt为例)可以通过以下步骤完成:---###**一、准备工作**1.**确保域名已解析**...

    取消回复欢迎 发表评论: