服务器:Tomcat 7
春天:版本4
Spring Security版本3.25
Tuckey:4.0.3
我正在尝试使用我的spring应用程序使用tuckey url rewriter,以便更好地支持angularjs html5mode。
我想要的是将任何请求转发给/如果request-uri不是/ api或/ socket。如果请求是/ api或/ socket我希望他们通过spring和dispather servlet并应用spring web security。
这是我的webintializer类: 公共类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">/$1</to>
</rule>
</urlrewrite>
我得到的错误如下。它似乎是在tuckey内某处的一些递归。如果我删除tuckey一切正常 线程中的例外&#34; http-bio-8080-exec-1&#34; java.lang.StackOverflowError的 at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) 在org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)
答案 0 :(得分:2)
过滤器定义中的DispatcherType.FORWARD
导致规则以递归方式应用于自身。使用网址重写过滤器时,您只想将其映射到REQUEST
。
希望有所帮助