首页 > 其他分享 >SpringSecurity过滤器之ExceptionTranslationFilter

SpringSecurity过滤器之ExceptionTranslationFilter

时间:2023-04-22 15:45:44浏览次数:42  
标签:return request SpringSecurity redirectUrl 403 过滤器 logger ExceptionTranslationFil

ExceptionTranslationFilter是处理AuthenticationException(身份认证异常)和AccessDeniedException(权限异常)。ExceptionTranslationFilter用法和源码分析参考一文搞定 Spring Security 异常处理机制!

 

AuthenticationEntryPoint是处理AuthenticationException,默认实现是LoginUrlAuthenticationEntryPoint。

LoginUrlAuthenticationEntryPoint#commence(HttpServletRequest request, HttpServletResponse response,AuthenticationException authException):

public void commence(HttpServletRequest request, HttpServletResponse response,
		AuthenticationException authException) throws IOException, ServletException {
	if (!this.useForward) {
		// redirect to login page. Use https if forceHttps true
		String redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);
		this.redirectStrategy.sendRedirect(request, response, redirectUrl);
		return;
	}
	String redirectUrl = null;
	if (this.forceHttps && "http".equals(request.getScheme())) {
		// First redirect the current request to HTTPS. When that request is received,
		// the forward to the login page will be used.
		redirectUrl = buildHttpsRedirectUrlForRequest(request);
	}
	if (redirectUrl != null) {
		this.redirectStrategy.sendRedirect(request, response, redirectUrl);
		return;
	}
	String loginForm = determineUrlToUseForThisRequest(request, response, authException);
	logger.debug(LogMessage.format("Server side forward to: %s", loginForm));
	RequestDispatcher dispatcher = request.getRequestDispatcher(loginForm);
	dispatcher.forward(request, response);
	return;
}

默认重定向到登录页面。

 
 
 

AccessDeniedHandler是处理AccessDeniedException异常。默认实现是AccessDeniedHandlerImpl。

AccessDeniedHandlerImpl#handle(HttpServletRequest request, HttpServletResponse response,AccessDeniedException accessDeniedException)

public void handle(HttpServletRequest request, HttpServletResponse response,
		AccessDeniedException accessDeniedException) throws IOException, ServletException {
	if (response.isCommitted()) {
		logger.trace("Did not write to response since already committed");
		return;
	}
	if (this.errorPage == null) {
		logger.debug("Responding with 403 status code");
		response.sendError(HttpStatus.FORBIDDEN.value(), HttpStatus.FORBIDDEN.getReasonPhrase());
		return;
	}
	// Put exception into request scope (perhaps of use to a view)
	request.setAttribute(WebAttributes.ACCESS_DENIED_403, accessDeniedException);
	// Set the 403 status code.
	response.setStatus(HttpStatus.FORBIDDEN.value());
	// forward to error page.
	if (logger.isDebugEnabled()) {
		logger.debug(LogMessage.format("Forwarding to %s with status code 403", this.errorPage));
	}
	request.getRequestDispatcher(this.errorPage).forward(request, response);
}

默认是返回403。

标签:return,request,SpringSecurity,redirectUrl,403,过滤器,logger,ExceptionTranslationFil
From: https://www.cnblogs.com/shigongp/p/17343159.html

相关文章

  • SpringSecurity完整流程、如何查看具体的过滤器
    SpringSecurity完整流程SpringSecurity的原理其实就是一个过滤器链,内部包含了提供各种功能的过滤器。这里我们可以看看入门案例中的过滤器。 图中只展示了核心过滤器,其它的非核心过滤器并没有在图中展示。UsernamePasswordAuthenticationFilter:负责处理我们再登录页面填写了......
  • SpringSecurity入门案例准备工作、入门案例引入SpringSecurity
    SpringSecurity入门案例准备工作1.快速入门1.1准备工作我们先要搭建一个简单的SpringBoot工程1、设置父工程添加依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3......
  • SpringSecurity课程介绍、SpringSecurity课程简介
    课程介绍课程简介 SpringSecurity是Spring家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shiro丰富。一般来说中大型的项目都是使用SpringSecurity来做安全框架。小项目有Shiro的比较多,因为相比与SpringSecurity,Shiro......
  • Redis布隆过滤器的原理和应用场景,解决缓存穿透
    大家好,我是哪吒。一、布隆过滤器BloomFilter是什么布隆过滤器BloomFilter是一种专门用来解决去重问题的高级数据结果。实质就是一个大型位数组和几个不同的无偏hash函数,无偏表示分布均匀。由一个初值为零的bit数组和多个哈希函数组成,用来判断某个数据是否存在,它和HyperLogLog一样,不......
  • SpringSecurity
     https://www.cnblogs.com/SjhCode/p/SpringSecurity.htmlpermitAll() :无条件允许任何形式访问,不管你登录还是没有登录。anonymous() :允许匿名访问,也就是没有登录才可以访问。denyAll() :无条件决绝任何形式的访问。authenticated():只允许已认证的用户访问。fullyAuthe......
  • Spring MVC过滤器-ShallowEtagHeaderFilter
    评:ShallowEtagHeaderFilter是spring提供的支持ETag的一个过滤器,所谓ETag是指被请求变量的实体值,是一个可以与Web资源关联的记号,而Web资源可以是一个Web页,也可以是JSON或XML文档,服务器单独负责判断记号是什么及其含义,并在HTTP响应头中将其传送到客户端,以下是服务器端返回的格式:[......
  • Django框架模版渲染与过滤器使用
    前端模版使用过滤器,如时间格式化等'''都是在前端页面中使用'''#default 如果一个变量是false或者为空,使用给定的默认值。否则,使用变量的值。{{value|default:"nothing"}}#给value这个值设置一个默认值,如果value没有传值或者值为空的话就显示nothing#length 返回值的......
  • 拦截器,过滤器,监听器区别
    1:本处讨论和语言无关 2:监听器一般是内部target对象触发event事件,常见设计上使用EventManger,event,eventAction三个对象封装处理,原则上使用观察者模式 3:拦截器和过滤器一般都是外部触发, 拦截器是拦截行为,过滤器是过滤数据,原则上使用代理模式......
  • 接口幂等性设计-拦截器+过滤器+redis
    接口幂等性设计-拦截器+过滤器+redis所需依赖:<!--redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>......
  • 爬取的数据存mysql中、加代理,cookie,header,加入selenium、布隆过滤器、scrapy-redis实
    上节回顾#1scrapy架构 -爬虫:写的一个个类-引擎: -调度器:排队,去重-下载器-pipline-下载中间件-爬虫中间件#2命令 -scrapystartproject项目名-scrapygensipder爬虫名网址-scrapycrawl爬虫名字-run.py#......