为什么Spring boot请求被Filter过滤器过滤了之后还会继续执行Interceptor拦截器中的方法?

November 23, 2022 作者: yijianhao 分类: javaSpringBoot 浏览: 310 评论: 0

这篇文章未经验证,请仔细辨别,以实际情况为主

Filter过滤器是Servlet级别的,不属于Spring,一般而言,被过滤器过滤掉的请求是不会再经过Interceptor拦截器的。

那为什么Spring boot请求被Filter过滤器过滤了之后还会继续执行Interceptor拦截器中的方法?

检查你是否调用了response.sendError(sc)方法。在官方文档中的描述:

Sends an error response to the client using the specified status code and clears the buffer. The server will preserve cookies and may clear or update any headers needed to serve the error page as a valid response. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back the error page

其中 If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back the error page,如果错误页被定义了,就会去返回这个错误页。

也就是说,Interceptor中的方法之所以还会响应,是因为spring boot将请求重定向到了错误响应中,Interceptor拦截器拦截的是错误页/error的请求


评论