使用SSH2和URLrewrite并获取stackoverflow错误

时间:2015-03-26 19:55:04

标签: java stack-overflow tuckey-urlrewrite-filter

我正在使用urlrewritefilter 4.0.3,我总是遇到stackoverflow错误。这是我的配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    <init-param>
        <param-name>statusEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/annotation/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>


<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

这是我的网址重写模式:

<rule>
    <note>
        Redirect find annotation by object id.
    </note>
    <from>/annotation/([a-z0-9]+)</from>
    <to type="forward">/annotation/getAnnotationByObjectID.action?oid=$1</to>
</rule>

一旦我运行它,我总是得到:

javax.servlet.ServletException: Filter execution threw an exception
org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
    org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
    org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
...
root cause

java.lang.StackOverflowError
    javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:403)
    javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:403)
    javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:403)
    javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:403)

我很生气。如果你知道它是怎么回事,请帮助我。非常感谢。

2 个答案:

答案 0 :(得分:4)

<from>规则中的正则表达式未锚定。显然,规则正在寻找URL内的任何匹配,因此如果它们没有锚定(开头有^而末尾有$),它们将匹配URL内的任何内容。

因此,您的目标网址/annotation/getAnnotationByObjectID.action?oid=$1实际上符合您的from规则(部分/annotation/get)。所以它在无限循环中一遍又一遍地运行相同的规则。

解决方案应该是锚定规则:

<from>^/annotation/([a-z0-9]+)$</from>

答案 1 :(得分:1)

正则表达式与URL不匹配,它会尝试查找模式。见https://code.google.com/p/urlrewritefilter/issues/detail?id=20

public class RewriteSanityCheck {

    public static void main(String[] args) {
        String from = "/annotation/([a-z0-9]+)";
        String to = "/annotation/getAnnotationByObjectID.action?oid=$1";

        Pattern pattern = Pattern.compile(from);

        if (pattern.matcher(to).find()) {
            System.out.println("Infinite loop");
        } else {
            System.out.println("OK");
        }
    }

}