Search code examples
javastack-overflowtuckey-urlrewrite-filter

Using SSH2 and URLrewrite and get stackoverflow error


I am using urlrewritefilter 4.0.3, I always got stackoverflow error. Here is my config:

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

Here is my url rewrite pattern:

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

Once I run it I always got :

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)

I am mad about it. Please if you know how it is, help me. Thanks a lot.


Solution

  • Your regular expression in the <from> rule is not anchored. Apparently, the rules are looking for any match inside the URL, so if they are not anchored (have a ^ at the beginning and a $ at the end), they will match anything inside the URL.

    As a result, your target URL /annotation/getAnnotationByObjectID.action?oid=$1 actually matches your from rule (The part /annotation/get does). So it runs the same rule over and over again in an endless loop.

    The solution should be to anchor the rule:

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