Spring mvc Spring 4升级中断错误页面筛选器链
场景:Spring mvc Spring 4升级中断错误页面筛选器链,spring-mvc,Spring Mvc,场景: 我们有一个拦截器,它在URL中查找虚假属性,如果找到,则抛出一个NoSuchRequestHandlingMethodException。然后我们显示一个自定义404页面 所有页面都通过相同的过滤器链来设置本地请求状态,记录一些信息,然后显示请求的页面。在Spring4中,在本例中,它停止了404页面的过滤链。如果你进入一个完全虚假的页面,404仍然可以运行,但是当我们抛出NoSuchRequestHandlingMethodException时,过滤器不会发生 春天三号: 1.为主请求
我们有一个拦截器,它在URL中查找虚假属性,如果找到,则抛出一个
NoSuchRequestHandlingMethodException
。然后我们显示一个自定义404页面
所有页面都通过相同的过滤器链来设置本地请求状态,记录一些信息,然后显示请求的页面。在Spring4中,在本例中,它停止了404页面的过滤链。如果你进入一个完全虚假的页面,404仍然可以运行,但是当我们抛出NoSuchRequestHandlingMethodException
时,过滤器不会发生
春天三号:1.为主请求运行筛选链
2.我们抛出
NoSuchRequestHandlingMethodException
3.过滤链终饰
4.新的过滤器链启动
5.我们记录错误页面度量值
6.我们向客户展示了一个漂亮的404页面 春天四号:
1.为主请求运行筛选链
2.我们抛出
NoSuchRequestHandlingMethodException
3.过滤链终饰
4.我们尝试记录错误页面指标,但由于第二个筛选链从未启动,因此NPE
5.我们向客户展示了一张糟糕的空白页 web.xml中的筛选代码:
<!-- The filter that captures the HttpServletRequest and HttpServletResponse-->
<filter>
<filter-name>ServletObjectFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>xxxxxxx.servletObjectFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ServletObjectFilter</filter-name>
<servlet-name>springmvc</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
...
<error-page>
<error-code>404</error-code>
<location>/errors/404</location>
</error-page>
ServletContainer:
static final ThreadLocal< HttpServletRequest > REQUESTS = new ThreadLocal< HttpServletRequest >();
static final ThreadLocal< HttpServletResponse > RESPONSES = new ThreadLocal< HttpServletResponse >();
public void setServletObjects( HttpServletRequest request, HttpServletResponse response ) {
REQUESTS.set( request );
RESPONSES.set( response );
}
public void removeAll() {
REQUESTS.remove();
RESPONSES.remove();
}
我“解决”了这个问题,将我的错误页面@Controller分为两部分,一部分是内部重定向的目标,不获取过滤链,另一部分是直接加载,并获取过滤链。然后我将redirect@Controller添加到拦截器黑名单中,因此它不需要来自过滤器的任何逻辑或数据。它解决了这个特定的问题,但我担心我的代码库中的其他东西也依赖于此行为。您的问题令人困惑。您使用的是过滤器还是拦截器?只有一个过滤链,所以你不会开始一个新的过滤链(除非你自己破解一些东西?)。发布一些代码,因为您现在只有web.xml的一部分。我们同时使用过滤器和拦截器。我们使用过滤器将所需的状态注入Spring,然后在拦截器中使用该状态。也许是我们自己动手做的事情,让Spring4停止了工作。不幸的是,web.xml有300多行,希望我添加了更多有用的代码。我添加了在Spring3上运行两次、在Spring4上只运行一次的过滤器,以及在Spring4上初始化拦截器失败的BeanPostProcessor,因为它假设过滤器已经运行。
static final ThreadLocal< HttpServletRequest > REQUESTS = new ThreadLocal< HttpServletRequest >();
static final ThreadLocal< HttpServletResponse > RESPONSES = new ThreadLocal< HttpServletResponse >();
public void setServletObjects( HttpServletRequest request, HttpServletResponse response ) {
REQUESTS.set( request );
RESPONSES.set( response );
}
public void removeAll() {
REQUESTS.remove();
RESPONSES.remove();
}
public class RequestResponseAwareBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization( Object bean, String beanName ) {
...
if ( bean instanceof RequestAware ) {
HttpServletRequest request = getServletContainer().getRequest();
if ( request == null ) {
throw new IllegalStateException( "The request object is NULL" );
}
RequestAware requestAware = (RequestAware) bean;
requestAware.setRequest( request );
}
}