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

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

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

    相关推荐

    F5负载均衡器如何通过irules实现应用的灵活转发?

    F5是非常强大的商业负载均衡器。除了处理性能强劲,以及高稳定性之外,F5还可以通过irules编写强大灵活的转发规则,实现web业务的灵活应用。irules是基于TCL语法的,每个iRules必须包含...

    映射域名到NAS

    前面介绍已经将域名映射到家庭路由器上,现在只需要在路由器上设置一下端口转发即可。假设NAS在内网的IP是192.168.1.100,NAS管理端口2000.你的域名是www.xxx.com,配置外部端...

    转发(Forward)和重定向(Redirect)的区别

    转发是服务器行为,重定向是客户端行为。转发(Forward)通过RequestDispatcher对象的forward(HttpServletRequestrequest,HttpServletRe...

    SpringBoot应用中使用拦截器实现路由转发

    1、背景项目中有一个SpringBoot开发的微服务,经过业务多年的演进,代码已经累积到令人恐怖的规模,亟需重构,将之拆解成多个微服务。该微服务的接口庞大,调用关系非常复杂,且实施重构的人员大部分不是...

    公司想搭建个网站,网站如何进行域名解析?

    域名解析是将域名指向网站空间IP,让人们通过注册的域名可以方便地访问到网站的一种服务。IP地址是网络上标识站点的数字地址,为方便记忆,采用域名来代替IP地址标识站点地址。域名解析就是域名到IP地址的转...

    域名和IP地址什么关系?如何通过域名解析IP?

    一般情况下,访客通过域名和IP地址都能访问到网站,那么两者之间有什么关系吗?本文中科三方针对域名和IP地址的关系和区别,以及如何实现域名与IP的绑定做下介绍。域名与IP地址之间的关系IP地址是计算机的...

    分享网站域名301重定向的知识

    网站域名做301重定向操作时,一般需要由专业的技术来协助完成,如果用户自己在维护,可以按照相应的说明进行操作。好了,下面说说重点,域名301重定向的操作步骤。首先,根据HTTP协议,在客户端向服务器发...

    NAS外网到底安全吗?一文看懂HTTP/HTTPS和SSL证书

    本内容来源于@什么值得买APP,观点仅代表作者本人|作者:可爱的小cherry搭好了NAS,但是不懂做好网络加密,那么隐私泄露也会随时发生!大家好,这里是Cherry,喜爱折腾、玩数码,热衷于分享数...

    ForwardEmail免费、开源、加密的邮件转发服务

    ForwardEmail是一款免费、加密和开源的邮件转发服务,设置简单只需4步即可正常使用,通过测试来看也要比ImprovMX好得多,转发近乎秒到且未进入垃圾箱(仅以Mailbox.org发送、Out...

    使用CloudFlare进行域名重定向

    当网站变更域名的时候,经常会使用域名重定向的方式,将老域名指向到新域名,这通常叫做:URL转发(URLFORWARDING),善于使用URL转发,对SEO来说非常有用,因为用这种方式能明确告知搜索引...

    要将端口5002和5003通过Nginx代理到一个域名上的操作笔记

    要将端口5002和5003通过Nginx代理到域名www.4rvi.cn的不同路径下,请按照以下步骤配置Nginx:步骤说明创建或编辑Nginx配置文件通常配置文件位于/etc/nginx/sites...

    SEO浅谈:网站域名重定向的三种方式

    在大多数情况下,我们输入网站访问网站的时候,很难发现www.***.com和***.com的区别,因为一般的网站主,都会把这两个域名指向到同一网站。但是对于网站运营和优化来说,www.***.com和...

    花生壳出现诊断域名与转发服务器ip不一致的解决办法

    出现诊断域名与转发服务器ip不一致您可以:1、更改客户端所处主机的drs为223.5.5.5备用dns为119.29.29.29;2、在windows上进入命令提示符输入ipconfig/flush...

    涨知识了!带你认识什么是域名

    1、什么是域名从技术角度来看,域名是在Internet上解决IP地址对应的一种方法。一个完整的域名由两个或两个以上部分组成,各部分之间用英文的句号“.”来分隔。如“abc.com”。其中“com”称...

    域名被跳转到其他网站是怎么回事

    当你输入域名时被跳转到另一个网站,这可能是由几种原因造成的:一、域名可能配置了域名转发服务。无论何时有人访问域名,比如.com、.top等,都会自动重定向到另一个指定的URL,这通常是在域名注册商设...

    取消回复欢迎 发表评论: