前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springsecurity之ExceptionTranslationFilter

Springsecurity之ExceptionTranslationFilter

作者头像
克虏伯
发布2019-04-15 10:20:33
2.1K0
发布2019-04-15 10:20:33
举报

    Springsecurity的版本是4.3.x,源码可以在Github上下载。

1、ExceptionTranslationFilter的doFilter

 ExceptionTranslationFilter是个异常过滤器,用来处理在认证授权过程中抛出的异常,ExceptionTranslationFilter后面的过滤器是FilterSecurityInterceptor。先上一张图,如下图1所示:

                                                              图1

  • 红框1中的,是调用Filter链中的后续Filter。
  • 如果图1中的操作抛出异常,就会来到红框2处,判断抛出的异常是否是AuthenticationException。
  • 如果抛出的异常不是AuthenticationException,即红框2的结果为null,那么就到红框3处,判断是否是AccessDeniedException。
  • 如果抛出的异常是AuthenticationException或者时AccessDeniedException,那么执行红框4处的代码。

2、ExceptionTranslationFilter的handleSpringSecurityException方法

下面来看handleSpringSecurityException的方法体,如下List-1所示

List-1

代码语言:javascript
复制
	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);

			sendStartAuthentication(request, response, chain,
					(AuthenticationException) exception);
		}
		else if (exception instanceof AccessDeniedException) {
			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.handle(request, response,
						(AccessDeniedException) exception);
			}
		}
	}
  1. 如果抛出的异常是AuthenticationException,则执行方法sendStartAuthentication
  2. 如果抛出的异常是AccessDeniedException,且从SecurityContextHolder.getContext().getAuthentication()得到的是AnonymousAuthenticationToken或者RememberMeAuthenticationToken,那么执行sendStartAuthentication
  3. 如果上面的第二点不满足,则执行accessDeniedHandler的handle方法

3、ExceptionTranslationFilter的sendStartAuthentication方法

    如下List-2所示,会调用authenticationEntryPoint的commence方法。

List-2

代码语言:javascript
复制
	protected void sendStartAuthentication(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain,
			AuthenticationException reason) throws ServletException, IOException {
		// SEC-112: Clear the SecurityContextHolder's Authentication, as the
		// existing Authentication is no longer considered valid
		SecurityContextHolder.getContext().setAuthentication(null);
		requestCache.saveRequest(request, response);
		logger.debug("Calling Authentication entry point.");
		authenticationEntryPoint.commence(request, response, reason);
	}

思考:

  1. 我们有时候会在xml配置中配置accessDeniedHandler,是在这里用到吗?
  2. List-2中使用到的authenticationEntryPoint,是什么?

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/11/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、ExceptionTranslationFilter的doFilter
  • 2、ExceptionTranslationFilter的handleSpringSecurityException方法
  • 3、ExceptionTranslationFilter的sendStartAuthentication方法
  • 思考:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档