spring security过滤器链及认证流程

一、过滤器链

spring Security功能的实现主要是由一系列过滤器链相互配合完成。
在这里插入图片描述下面介绍过滤器链中主要的几个过滤器及其作用:

1.SecurityContextPersistenceFilter 会在请求开始时从配置好的 SecurityContextRepository 中获取 SecurityContext,然后

把它设置给 SecurityContextHolder。在请求完成后将 SecurityContextHolder 持有的 SecurityContext 再保存到配置好的

SecurityContextRepository,同时清除 securityContextHolder 所持有的 SecurityContext;

2.UsernamePasswordAuthenticationFilter 用于处理来自表单提交的认证。该表单必须提供对应的用户名和密码,其内部还有

登录成功或失败后进行处理的 AuthenticationSuccessHandler 和 AuthenticationFailureHandler,这些都可以根据需求做相

关改变;

3.ExceptionTranslationFilter 能够捕获来自 FilterChain 所有的异常,并进行处理。

但是它只会处理两类异常:AuthenticationException 和 AccessDeniedException,其它的异常它会继续抛出。

— 如果捕获到的是 AuthenticationException,那么将会使用其对应的 AuthenticationEntryPoint 的commence()处理。在处

理之前,ExceptionTranslationFilter先使用 RequestCache 将当前的HttpServerletRequest的信息保存起来,以至于用户成功

登录后可以跳转到之前的界面;

— 如果捕获到的是 AccessDeniedException,那么将视当前访问的用户是否已经登录认证做不同的处理,如果未登录,则会使

用关联的 AuthenticationEntryPoint 的 commence()方法进行处理,否则将使用关联的 AccessDeniedHandler 的handle()方

法进行处理。

说明:

①AuthenticationEntryPoint 是在用户没有登录时用于引导用户进行登录认证的;

②AccessDeniedHandler 用于在用户已经登录了,但是访问其自身没有权限的资源时做出对应的处理 [默认实现类:

AccessDeniedHandlerImpl];

③RequestCache [默认实现类:HttpSessionRequestCache]会将 HttpServletRequest 相关信息封装为一个

SavedRequest 并保存到 HttpSession中;

4.FilterSecurityInterceptor 是用于保护Http 资源的,它需要一个AccessDecisionManager和一个AuthenticationManager 的

引用。它会从 SecurityContextHolder 获取 Authentication,然后通过 SecurityMetadataSource 可以得知当前请求是否在请

求受保护的资源。对于请求那些受保护的资源,如果Authentication.isAuthenticated()返回false或者FilterSecurityInterceptor

的alwaysReauthenticate 属性为 true,那么将会使用其引用的 AuthenticationManager 再认证一次,认证之后再使用认证后

的 Authentication 替换 SecurityContextHolder 中拥有的那个。然后就是利用 AccessDecisionManager 进行权限的检查;

二、认证流程

认证流程分为登录流程和注销流程,下面将一一叙述。

(注意:这里的登录方式为"帐号+密码";箭头代表整个流程执行方向)

1)登录流程

—> SecurityContextPersistenceFilter(作用在第一节已经介绍)

—> UsernamePasswordAuthenticationFilter

(先获取用户名和密码,并将其封装成UsernamePasswordToken,然后调用AuthenticationManager进行验证)

—> AuthenticationManager

(根据token类型选择合适的AuthenticationProvider来处理认证请求) [默认实现类:ProviderManager]

AuthenticationManager是一个用来处理请求的接口,它自己不直接处理认证请求,而是委托给其所配置的Authentication

Provider列表,然后会依次使用每一个 AuthenticationProvider 进行认证,如果有一个AuthenticationProvider 认证后的结果

不为 null,则表示该AuthenticationProvider已经认证成功,之后的AuthenticationProvider 将不再继续认证。然后直接以该

AuthenticationProvider 的认证结果作为 ProviderManager 的认证结果。如果所有的 AuthenticationProvider 的认证结果都

为null,则表示认证失败,将抛出一个 ProviderNotFoundException。

—> AuthenticationProvider

(请求认证处理) [默认实现类:DaoAuthencationProvider]

DaoAuthenticationProvider认证过程:

DaoAuthenticationProvider先调用UserDetailsService 的loadUserByUsername()方法获取UserDetails,获取后再与

UsernamePasswordAuthenticationFilter获取的username和password进行比较;如果认证通过后会将该 UserDetails 赋给认

证通过的 Authentication的principal,然后再把该 Authentication 存入到 SecurityContext 中。默认情况下,在认证成功后

ProviderManager也将清除返回的Authentication中的凭证信息。

注意在这里面根据需要增加[自定义关键类(UserDetailService):实现UserDetailService接口并复写loadUserByUsername()]

问:为什么AuthenticationManager不直接认证请求?

答:因为token有多种类型。比如最简单的UsernamePasswordAuthenticationToken,还有spring social的token;

—> Authentication对象

Spring Security使用一个Authentication 对象来描述当前用户的相关信息。SecurityContextHolder中持有的是当前用户的

SecurityContext,而 SecurityContext 持有的是代表当前用户相关信息的 Authentication 的引用。这个 Authentication 对象

不需要我们自己去创建,在与系统交互的过程中,Spring Security会自动为我们创建相应的Authentication对象,然后赋值给当

前的SecurityContext。

2)注销流程

1、使HttpSession失效;

2、清除所有已经配置的remember-me认证;

3、清除SecurityContextHolder中的user信息,并设置Authentication中的Authenticated属性为false;
4、跳转到指定url;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值