首页 > 其他分享 >Spring Security – RequestRejectedException

Spring Security – RequestRejectedException

时间:2023-01-05 22:01:18浏览次数:54  
标签:RequestRejectedHandler spring Spring request RequestRejectedException ex request

概述

Spring Security 提供了 RequestRejectedHandler 来处理当请求被拒绝时候如何处理,在没有进行配置的情况下,默认是使用 DefaultRequestRejectedHandler 直接将异常进行抛出:

throw requestRejectedException;

同时也提供了 HttpStatusRequestRejectedHandler 来返回对应的状态码。

定制 RequestRejectedHandler

RequestRejectedHandler 的注入是在 WebSecuritysetApplicationContext 当中:

try {
    this.requestRejectedHandler = applicationContext.getBean(RequestRejectedHandler.class);
}
catch (NoSuchBeanDefinitionException ex) {
}
if (this.requestRejectedHandler != null) {
   filterChainProxy.setRequestRejectedHandler(this.requestRejectedHandler);
}

在中的定义为:

private RequestRejectedHandler requestRejectedHandler = new DefaultRequestRejectedHandler();

我们只需要覆盖 Bean 定义即可:

@Bean
RequestRejectedHandler requestRejectedHandler() {
   return new CustomizerRequestRejectedHandler();
}
@Slf4j
public class CustomizerRequestRejectedHandler implements RequestRejectedHandler {
   @Override
   public void handle(HttpServletRequest request, HttpServletResponse response,
   RequestRejectedException ex) throws IOException, ServletException {
      log.warn("request uri: {},user-agent: {}", request.getRequestURI(), request.getHeader(HttpHeaders.USER_AGENT));
      log.error(ex.getMessage(), ex);
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
   }
}

参考

spring-5-0-3-requestrejectedexception-the-request-was-rejected-because-the-url

log+request+uri+on+RequestRejectedException

how-to-intercept-a-requestrejectedexception-in-spring

spring-security-request-rejected-exception

标签:RequestRejectedHandler,spring,Spring,request,RequestRejectedException,ex,request
From: https://www.cnblogs.com/doug-shaw/p/spring-secuirty-request-rejected-exception.html

相关文章

  • 同事说不会在SpringBoot中集成日志记录操作我向他推荐了这篇文章
    SpringBoot中记录操作日志记一次SpringBoot中记录管理员的操作日志,记录管理员对用户、管理员、分类、视频等模块的操作,如:删除管理员、用户、视频、修改密码、信息等等,都......
  • 在SpringCloud中使用REST服务时的报错
    在SpringCloud中使用REST服务时使用前需要先在配置类中注入RestTemplate的Bean然后再使用自动装配即可@AutowiredprivateRestTemplaterestTemplate;问题......
  • SpringMVC实现浏览器端大文件分片上传
    ​ 最近遇见一个需要上传超大大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的......
  • springboot 资源分离 打包
    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</a......
  • spring中Bean生命周期
    1.普通java对象创建过程举例:BusinessServicebusinessService=newBusinessService();编译:将.java文件编译成class文件加载:等到类需要被初始化时(比如new)class文件被虚......
  • 【SpringBoot】创建项目与启动
     创建项目说明  查看JDK配置    ......
  • Spring bean注入问题:NoUniqueBeanDefinitionException解决方案归纳
    引言   spring实现的bean自动注入在项目开发中是一个经常使用到的功能,但自动装配两个或多个bean时,会抛出NoUniqueBeanDefinitionException:Noqualifyingbeanoftyp......
  • RESTful风格与Spring注解
    RESTfulL是一种网络应用程序的设计风格和开发方式,即接口请求方式和路径的一种风格。普通风格:localhost:8080/add?a=1&b=2RestFul风格:localhost:8080/add/1/2GET获......
  • Spring MVC
    1、​​SpringMVC—@RequestMapping原理讲解-1​​2、​​SpringMVC—@RequestMapping原理讲解-2​​3、​​SpringMVC判定返回view的依据​​4、​​SpringMVC源......
  • Spring源码分析
    一、Java注解​​全面解析JAVA注解​​​​JAVA注解的基本原理​​​​秒懂,Java注解(Annotation)你可以这样学​​​​Java编译时注解处理器(APT)详解​​二、Java反射​​Ja......