Spring Security默认拦截器链解析(十三)

1.SessionManagementFilter 会话管理器

见名知意,会话管理器。主要是管理SecurityContext到会话中去。

主要内容:

1.防止重复执行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pe2L0U6L-1626242770530)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e26f0c5a-a8b0-4f7f-b45d-8ea80f916399/Untitled.png)]

2.sessionAuthenticationStrategy session认证处理策略

sessionAuthenticationStrategy主要是用于在非匿名身份认证时,可以自定义策略去对HttpSession进行相关操作。
典型用途是确保会话存在或更改会话 ID 以防止会话固定攻击ChangeSessionIdAuthenticationStrategy和SessionFixationProtectionStrategy。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ndRfagIQ-1626242770532)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/08088eb9-0dfe-4742-b517-d868d8d28874/Untitled.png)]

3.如果SecurityContext或者Authentication不存在

在这种情况下,将request中的session进行失效。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sogh7KyH-1626242770534)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/14cf2f81-3599-444a-a762-520a0868daae/Untitled.png)]



2.ExceptionTranslationFilter Spring Security异常处理器

这个主要是针对过Spring Security滤器链出现问题时,如何进行异常处理。在默认拦截器链中,主要是针对FilterSecurityInterceptor进行异常捕获。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-joY8EJRM-1626242770536)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7fc987f9-a934-4509-9db3-5d9a569bf17d/Untitled.png)]

如何处理SpringSecurity异常呢?

handleSpringSecurityException方法解析:

private void handleSpringSecurityException(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain, RuntimeException exception)
			throws IOException, ServletException {
		if (exception instanceof AuthenticationException) {
			logger.debug(
					"Authentication exception occurred; redirecting to authentication entry point",
					exception);
			// 清空SecurityContext中的Authentication
			// requestCache中缓存当前request
			// 转发或者重定向到登录
			sendStartAuthentication(request, response, chain,
					(AuthenticationException) exception);
		}
		else if (exception instanceof AccessDeniedException) {
				// 当异常为访问被拒时
				// 1.清空Authentication
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			if (authenticationTrustResolver.isAnonymous(authentication) || authenticationTrustResolver.isRememberMe(authentication)) {
				logger.debug(
						"Access is denied (user is " + (authenticationTrustResolver.isAnonymous(authentication) ? "anonymous" : "not fully authenticated") + "); redirecting to authentication entry point",
						exception);
				// 转发或者重定向到登录
				sendStartAuthentication(
						request,
						response,
						chain,
						new InsufficientAuthenticationException(
							messages.getMessage(
								"ExceptionTranslationFilter.insufficientAuthentication",
								"Full authentication is required to access this resource")));
			} 
			else {
				logger.debug(
						"Access is denied (user is not anonymous); delegating to AccessDeniedHandler",
						exception);
				// accessDeniedHandler 见过很多次了。就是到forbidden页面,或者error页面
				accessDeniedHandler.handle(request, response,
						(AccessDeniedException) exception);
			}
		}
	}

sendStartAuthentication 转发或者重定向到认证入口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UgWfS9AL-1626242770537)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0d7104cc-e19e-49f2-9792-55c89fbc8220/Untitled.png)]

accessDeniedHandler.handle 转发到error页面或者返回403 forbidden
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WuN5lBog-1626242770539)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/61bbd666-ef8a-4cb0-8878-7f98935c19ad/Untitled.png)]



3.小结

这篇看了一下关于Session的管理和异常处理。

关于SessionManagementFilter这边,我们可以通过SessionManagementConfigurer来知道如何配置

SessionManagementFilter。

这里简单列一下可以配置的内容:

  1. SecurityContextRepository SecurityContext存储
  2. getSessionAuthenticationStrategy 配置Session处理策略
  3. sessionAuthenticationErrorUrl session认证失败跳转url
  4. getInvalidSessionStrategy Session失效策略
  5. getSessionAuthenticationFailureHandler session认证失败处理器
  6. isConcurrentSessionControlEnabled 并发session控制器

而异常处理这边我们也可以自定义AccessDeniedHandler,自定义AuthenticationEntryPoint。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M4uGqWDv-1626242770541)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1c0c6833-f3cc-4251-9d17-32401a8433b1/Untitled.png)]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欢谷悠扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值