Spring 和 Tuckey URLRewriter StackOverflow 异常

Spring and Tuckey URLRewriter StackOverflow Exception

服务器:Tomcat7
Spring:版本 4
Spring 安全版本 3.25
图基:4.0.3

我正在尝试将 tuckey url 重写器与我的 spring 应用程序一起使用,以便更好地支持 angularjs html5 模式。

如果请求 uri 不是 /api 或 /socket,我想要将任何请求转发到 /。如果请求是 /api 或 /socket,我希望他们通过 spring 和 dispather servlet 并应用 spring 网络安全。

这是我的 webintializer class: public class ApplicationInitializer 实现 WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(PersistenceConfig.class, WebSecurityConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.register(ApplicationConfig.class);

    AnnotationConfigWebApplicationContext socketContext = new AnnotationConfigWebApplicationContext();
    webContext.register(WebSocketConfig.class);

    ServletRegistration.Dynamic restApi = servletContext.addServlet("rest-api", new DispatcherServlet(webContext));
    restApi.setLoadOnStartup(1);
    restApi.addMapping("/api");

    ServletRegistration.Dynamic webSocket = servletContext.addServlet("web-socket", new DispatcherServlet(socketContext));
    webSocket.setLoadOnStartup(1);
    webSocket.addMapping("/socket");

    Dynamic springSecurityFilter = servletContext.addFilter(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, DelegatingFilterProxy.class);
    springSecurityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/api/*");
    springSecurityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/socket/*");

    UrlRewriteFilter urlRewriteFilter = new UrlRewriteFilter();
    Map<String, String> urlRewriteFilterParams = new HashMap<String, String>();
    urlRewriteFilterParams.put("confReloadCheckInterval", "0");
    urlRewriteFilterParams.put("confPath", "/WEB-INF/classes/urlrewrite.xml");
    urlRewriteFilterParams.put("statusEnabled", "true");
    urlRewriteFilterParams.put("logLevel", "DEBUG");
    urlRewriteFilterParams.put("statusEnabledOnHosts", "localhost");

    Dynamic tuckeyRewriteFilter = servletContext.addFilter("UrlRewriteFilter", urlRewriteFilter);
    tuckeyRewriteFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
    tuckeyRewriteFilter.setInitParameters(urlRewriteFilterParams);

}
}

重写器正在被选中,因为我可以转到 localhost:8080/rewrite-status,我的配置在那里,页面上说它应该做我想做的事。

我的 Tuckey UrlRewrite.XML 文件如下:

  <!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite default-match-type="wildcard">
    <rule>
        <condition type="request-uri" operator="notequal">/api/**</condition>
        <condition type="request-uri" operator="notequal">/socket/**</condition>
        <from>/**</from>
        <to type="forward">/</to>
    </rule>
</urlrewrite>

我得到的错误如下。这似乎是 tuckey 中某处的某种递归性。如果我删除 tuckey 一切正常 线程异常 "http-bio-8080-exec-1" java.lang.WhosebugError 在 org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) 在 org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) 在 org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) 在 org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) 在 org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)

过滤器定义中的 DispatcherType.FORWARD 导致规则递归应用于自身。使用 URL 重写过滤器时,您只想将其映射到 REQUEST.

希望对您有所帮助