我试图在我的春季启动项目中使用Tuckey
作为urlRewrite,但我在注册Tuckey
过滤器时遇到错误:
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: unable to find urlrewrite conf file at `urlrewrite.xml` even i have `urlrewrite.xml` file under resources.
我在Configuration class
注册了Tucky过滤器:
@Bean
public FilterRegistrationBean urlRewriteFilterRegistration() throws IOException {
log.info("UrlRewriteFilter registered!");
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new UrlRewriteFilter());
registration.setName("UrlRewriteFilter");
List<String> urlPatterns = new ArrayList<>();
urlPatterns.add("/urlmap");
registration.setUrlPatterns(urlPatterns);
registration.getInitParameters().put("confPath","/urlrewrite.xml");
return registration;
}
即使我以前注册过滤器如下:
FilterRegistration.Dynamic urlRewriteFilter = servletContext.addFilter("urlRewriteFilter", new UrlRewriteFilter());
urlRewriteFilter.setInitParameter("confPath", "urlrewrite.xml");
urlRewriteFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
它在无启动版本中工作! 我的配置有什么问题。
答案 0 :(得分:2)
最近有同样的问题。 发现这篇文章的答案: Spring boot cannot find urlrewrite.xml inside jar file
TL; DR:创建了一个扩展UrlRewriteFilter的类并覆盖了loadUrlRewriterMethod以允许将配置加载为 org.springframework.core.io.Resource
请适应您的需求:
public class BootCompliantUrlRewriteFilter extends UrlRewriteFilter {
private Resource resource;
public BootCompliantUrlRewriteFilter(Resource config){
this.resource = config;
}
//Override the loadUrlRewriter method, and write your own implementation
@Override
protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
//Create a UrlRewrite Conf object with the injected resource
Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@yourOwnSystemId@@");
checkConf(conf);
} catch (IOException ex) {
throw new ServletException("Unable to load URL rewrite configuration file from " + this.resource, ex);
}
}
}
并在应用程序类中:
public static final String REWRITE_FILTER_NAME = "rewriteFilter";
public static final String REWRITE_FILTER_CONF_PATH = "urlrewrite.xml";
@Bean
public FilterRegistrationBean rewriteFilterConfig() {
FilterRegistrationBean reg = new FilterRegistrationBean();
reg.setName(REWRITE_FILTER_NAME);
reg.setFilter(new BootCompliantUrlRewriteFilter(new ClassPathResource(REWRITE_FILTER_CONF_PATH)));
//reg.addInitParameter("confPath", REWRITE_FILTER_CONF_PATH);
reg.addInitParameter("confReloadCheckInterval", "-1");
reg.addInitParameter("statusPath", "/redirect");
reg.addInitParameter("statusEnabledOnHosts", "*");
reg.addInitParameter("logLevel", "WARN");
return reg;
}