【问题标题】:Spring boot cannot find urlrewrite.xml inside jar fileSpring Boot 在 jar 文件中找不到 urlrewrite.xml
【发布时间】:2015-09-09 18:30:51
【问题描述】:

我正在使用带有嵌入式 Tomcat 的 Spring Boot,并且类 UrlRewriteFilter 找不到配置文件 urlrewrite.xml,该类使用 servletcontext.getResourceAsStream(this.confPath),我在其他文章中读到此方法不当包是 jar 时不起作用。有人遇到过这个问题吗?

【问题讨论】:

标签: mod-rewrite tuckey-urlrewrite-filter


【解决方案1】:

发现于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);
        }
    }
}

【讨论】:

    【解决方案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();
    }
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

      这在使用 'mvn spring-boot:run' 时有效,我认为这可以满足您在 jar 中运行的需要。在我的项目中,我构建了一个战争文件,这也适用。

      【讨论】:

        【解决方案4】:

        我并不为此感到自豪,但这对我有用。库本身在某一时刻使用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);
        

        【讨论】:

          猜你喜欢
          • 2018-08-30
          • 1970-01-01
          • 2021-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多