在这里使用弹簧框架...

我创建了一个过滤器以更改CSS文件的响应正文,如果我直接调用URL运行。但是,如果匹配URLREWRITE规则,则跳过筛选器。

示例: 在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中有一个映射设置:

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

(我们需要多种原因)

so,任何* .css文件,它的路径从w / styles-special /“将被重写为”/样式/“,并且不会调用CSSFilter,但是路径开始的任何* .css文件/“/样式/”将按预期运行CSSFilter。

我尝试将cssfilter的URL模式更改为许多不同选项,但结果相同。它看起来像是Tuckey UrlRewrite过滤器只是在重写后不调用Chain.dofilter(),但也许它比这更复杂?

任何想法在这里可能是什么问题?这是预期的功能吗?任何解决方法? ...也许拦截器或控制器是去这里的方式?

提前感谢您的任何建议!


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

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

修复问题W /链接,滤波器运行。但是,我收到以下错误:

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)
.

从过滤器中的代码片段:

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
...
.

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top