我正在开发一个带有过滤器的 Grails 插件,代码如下:
void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, javax.servlet.FilterChain chain) {
HttpServletRequest request = servletRequest
HttpServletResponse response = servletResponse
String url = request.requestURI.substring(request.contextPath.length())
logger.debug("Processing url: $url")
chain.doFilter(servletRequest, servletResponse)
}
非常标准的 Java EE 过滤器,它扩展了 spring 的GenericFilterBean
. 它是由SpringSecurityUtils.registerFilter %NAME%, %POS%
(它是 spring 安全扩展)注册的
升级到 Grails 2.0 后,我开始chain.doFilter()
在线收到一些奇怪的错误:
No thread-bound request found: Are you referring to request attributes outside of
an actual web request, or processing a request outside of the originally receiving
thread? If you are actually operating within a web request and still receive this
message, your code is probably running outside of DispatcherServlet/DispatcherPortlet:
In this case, use RequestContextListener or RequestContextFilter to expose the
current request.
请注意,一切正常String url = request.requestURI.....
。并且显示的 url 是有效的。
这个错误不可预测的问题:(有时过滤器工作正常,有时会抛出异常,我不知道是什么问题,以及我必须做什么来解决它。可能这取决于bean初始化的顺序,但我'我不确定
更新:发现是因为resources
插件。删除此插件后错误消失。不确定这是不是最好的方法。