tl; dr:如何为 Spring Boot 错误页面启用 Spring 的 ResourceUrlEncodingFilter?
(使用 spring boot 1.3.7.RELEASE 和 Spring Framework/MVC 4.2.4.RELEASE 时写的问题)
一些背景知识:我们有一个相当标准的 spring boot/spring webmvc 项目,使用 Thymeleaf 作为视图层。我们启用了开箱即用的 spring boot 资源链来服务静态资产。
我们的 thymeleaf 视图具有标准的 url 编码语法,例如<script th:src="@{/js/some-page.js}"></script>
. 这依赖于 Springorg.springframework.web.servlet.resource.ResourceUrlEncodingFilter
将 url 转换为适当版本的 url,例如/v1.6/js/some-page.js
.
我们的错误处理是通过以下方式完成的:
- 环境
server.error.whitelabel.enabled=false
- 子类化spring boot的默认值
BasicErrorController
以覆盖public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response)
- 依靠我们已经配置的 thymeleaf 视图解析器来呈现我们的自定义错误页面
问题是:ResourceUrlEncodingFilter 没有应用于我们的错误页面。我认为缺少为 ERROR 调度请求注册的过滤器,但这对我来说并不明显:a)如何在 spring boot 中自定义它;b)为什么默认情况下没有这样做。
更新1:
问题似乎与OncePerRequestFilter
ERROR 调度程序的组合有关。即:
ResouceUrlEncodingFilter
默认情况下不绑定到 ERROR 调度程序。虽然覆盖这很混乱,但这并非不可能,但由于以下原因无济于事:OncePerRequestFilter
(parent ofResourceUrlEncodingFilter
) 在 Request 上设置一个属性,表明它已被应用,以便不重新应用。然后它包装响应对象。但是,当发送 ERROR 时,不会使用包装的响应,并且由于请求属性仍然存在,过滤器不会重新包装。
更糟糕的是,定制的逻辑boolean hasAlreadyFilteredAttribute
不能被请求覆盖。OncePerRequestFilter
的doFilter()
方法是final,并且getAlreadyFilteredAttributeName()
(扩展点)无权访问当前请求对象来获取调度程序。
我觉得我一定错过了什么;在 Spring Boot 的 404 页面上使用版本化资源似乎是不可能的。
更新 2:一个有效但混乱的解决方案
这是我能想到的最好的,但看起来仍然非常混乱:
public abstract class OncePerErrorRequestFilter extends OncePerRequestFilter {
@Override
protected String getAlreadyFilteredAttributeName() {
return super.getAlreadyFilteredAttributeName() + ".ERROR";
}
@Override
protected boolean shouldNotFilterErrorDispatch() {
return false;
}
}
public class ErrorPageCapableResourceUrlEncodingFilter extends OncePerErrorRequestFilter {
// everything in here is a perfect copy-paste of ResourceUrlEncodingFilter since the internal ResourceUrlEncodingResponseWrapper is private
}
// register the error-supporting version if the whitelabel error page has been disabled ... could/should use a dedicated property for this
@Configuration
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@ConditionalOnClass(OncePerErrorRequestFilter.class)
@ConditionalOnWebApplication
@ConditionalOnEnabledResourceChain
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", havingValue="false", matchIfMissing = false)
public static class ThymeleafResourceUrlEncodingFilterErrorConfiguration {
@Bean
public FilterRegistrationBean errorPageResourceUrlEncodingFilterRegistration() {
FilterRegistrationBean reg = new FilterRegistrationBean();
reg.setFilter(new ErrorPageCapableResourceUrlEncodingFilter());
reg.setDispatcherTypes(DispatcherType.ERROR);
return reg;
}
}
更好的解决方案?