Spring引导在jar文件中找不到urlrewrite.xml

时间:2015-06-23 19:16:39

标签: mod-rewrite tuckey-urlrewrite-filter

我正在使用带有嵌入式Tomcat的Spring Boot,而类UrlRewriteFilter找不到配置文件urlrewrite.xml,这个类使用servletcontext.getResourceAsStream(this.confPath),我在其他文章中读到这个方法没有当包装是一个罐子时工作。有人有这个问题吗?

4 个答案:

答案 0 :(得分:3)

blog post

中找到
import org.springframework.core.io.Resource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

//Adding @Component Annotation to a Filter is enough to register the Filter, when you have no web.xml
@Component
public class MyUrlRewriteFilter extends UrlRewriteFilter {

    private static final String CONFIG_LOCATION = "classpath:/urlrewrite.xml";

    //Inject the Resource from the given location
    @Value(CONFIG_LOCATION)
    private Resource resource;

    //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 " + CONFIG_LOCATION, ex);
        }
    }
}

答案 1 :(得分:2)

以下代码对我有用。

请使用以下依赖项:

         <dependency>
           <groupId>org.tuckey</groupId>
           <artifactId>urlrewritefilter</artifactId>
           <version>4.0.4</version>
         </dependency>

在资源文件夹中创建urlrewrite.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite>
    <rule>
        <name>Domain Name Check</name>
        <condition name="host" operator="notequal">www.userdomain.com</condition>
        <from>^(.*)$</from>
        <to type="redirect">http://www.userdomain.com$1</to>
    </rule>
</urlrewrite>

在主ApplicationRunner.java中添加以下内容:

@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new CustomURLRewriter());
    return registrationBean;
}

创建CustomURLRewriter:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
import javax.servlet.*;
import java.io.InputStream;

public class CustomURLRewriter extends UrlRewriteFilter {
private UrlRewriter urlRewriter;

@Autowired
Environment env;

@Override
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
    try {
        ClassPathResource classPathResource = new ClassPathResource("urlrewrite.xml");
        InputStream inputStream = classPathResource.getInputStream();
        Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "urlrewrite.xml", "");
        urlRewriter = new UrlRewriter(conf1);
    } catch (Exception e) {
        throw new ServletException(e);
    }
}

@Override
public UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) {
    return urlRewriter;
}

@Override
public void destroyUrlRewriter() {
    if(urlRewriter != null)
        urlRewriter.destroy();
}
}

答案 2 :(得分:0)

我正在使用带有Spring Boot的Tuckey UrlRewriteFilter。我按照stack overflow question中的初始问题实现(不是答案)。 我还添加了

registrationBean.addInitParameter("confPath", "urlrewrite.xml");

为urlrewrite.xml文件设置特定路径。 在项目中,我将文件放在src / main / webapp。

这在使用'mvn spring-boot:run'时有效,我认为它符合你在jar中运行的需要。在我的项目中,我构建了一个war文件,这也适用。

答案 3 :(得分:0)

我并不为此感到自豪,但这对我有用。库本身一次使用ClassLoader.getSystemResourceAsStream()来尝试解析文件名,所以我认为除非库有一天更新/更改,否则这将有效。

String fileName = "urlrewrite.xml";
// The prefixed version seems to be needed when running within a jar. 
// Rumor has it that it may be a tomcat 8 specific.
String confPath = java.lang.ClassLoader.getSystemResourceAsStream(fileName) != null ? fileName : "/WEB-INF/classes/" + fileName;
registrationBean.addInitParameter("confPath", confPath);