为什么Tuckey UrlRewrite Filter 在匹配规则后不调用chain.doFilter? [英] Why Doesn't Tuckey UrlRewrite Filter call chain.doFilter after a rule is matched?

查看:40
本文介绍了为什么Tuckey UrlRewrite Filter 在匹配规则后不调用chain.doFilter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里使用 Spring 框架...

Using Spring Framework here...

我创建了一个过滤器来更改 css 文件的响应正文,如果我直接调用 url,它会运行.但是,如果匹配 urlrewrite 规则,则会跳过过滤器.

I created a filter to change the response body of css files and if I call a url directly it runs. However, if a urlrewrite rule is matched the filter is skipped.

示例:在 web.xml 中:

Example: In web.xml:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    <!-- skipping init params here for brevity -->
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>cssFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
    <filter-mapping>
    <filter-name>cssFilter</filter-name>
    <url-pattern>*css</url-pattern>
</filter-mapping>

在 urlrewrite.xml 中有一个这样的映射设置:

There is a mapping set up like this in urlrewrite.xml:

<rule>
    <from>/styles-special/(.*)$</from>
    <to last="true">/styles/$1</to>
</rule>

(我们需要这个有很多原因)

(we need this for a number of reasons)

因此,路径以/styles-special/"开头的任何 *.css 文件将被重写为/styles/",并且不会调用 cssFilter,但路径以 w 开头的任何 *.css 文件/"/styles/" 将按预期运行 cssFilter.

so, any *.css file whose path starts w/ "/styles-special/" will be rewritten to "/styles/" and the cssFilter won't be called, but any *.css file whose path starts w/ "/styles/" will run through the cssFilter as expected.

我尝试将 cssFilter 的 url-pattern 更改为许多不同的选项,但结果相同.在我看来,tuckey urlrewrite 过滤器在重写后不会调用 chain.doFilter(),但也许比这更复杂?

I've tried changing the url-pattern for cssFilter to a number of different options, but same result. It seems to me like the tuckey urlrewrite filter just doesn't call chain.doFilter() after a rewrite, but maybe it's more complicated than that?

知道这里可能有什么问题吗?这是预期的功能吗?任何解决方法?...也许拦截器或控制器是通往这里的路?

Any idea what the issue might be here? Is this expected functionality? Any workarounds? ...maybe an interceptor or controller is the way to go here?

提前感谢您对此的任何建议!!

Thanks in advance for any advice on this!!

注意:使用以下内容(根据 axtavt 的建议):

Note: Using the following (as suggested by axtavt):

<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>

修复了链接问题并运行过滤器.但是,我收到以下错误:

Fixes the issue w/ chaining and the Filter is run. However, I get the following error:

java.lang.IllegalStateException: NO CONTENT
at org.mortbay.jetty.HttpGenerator.addContent(HttpGenerator.java:106)
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:644)
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:579)

这是过滤器的代码片段:

Here's code snippet from Filter:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    OutputStream out = httpResponse.getOutputStream();
    GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);

    chain.doFilter(request, wrapper);

    if(log.isDebugEnabled()) log.debug("doFilter: chain");

    String respBody = new String(wrapper.getData()); // this throws error
...

推荐答案

当 Tuckey UrlRewrite 过滤器重写 URL 时,它会将请求转发到新 URL,而不是将其向下传递到过滤器链.默认情况下过滤器不会应用于转发的请求,因此您需要对其进行配置:

When Tuckey UrlRewrite Filter rewrites a URL, it forwards request to the new URL instead of passing it down the filter chain. By default filters are not applied to the forwarded requests, so you need to configure it:

<filter-mapping>
     <filter-name>cssFilter</filter-name>
     <url-pattern>*css</url-pattern>
     <dispatcher>REQUEST</dispatcher>
     <dispatcher>FORWARD</dispatcher>
</filter-mapping>

这篇关于为什么Tuckey UrlRewrite Filter 在匹配规则后不调用chain.doFilter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆