2015-06-23 43 views
2

我正在使用Spring Boot和嵌入式Tomcat,並且UrlRewriteFilter類找不到配置文件urlrewrite.xml,因此此類使用servletcontext.getResourceAsStream(this.confPath)而我在其他文章中讀到,當包是罐子時,這種方法不起作用。有人有這個問題嗎?Spring引導程序無法在jar文件中找到urlrewrite.xml

+0

這個問題https://github.com/spring-projects/spring-boot/issues/3627 – goat

回答

0

我在Spring Boot中使用了Tuckey UrlRewriteFilter。我遵循這個stack overflow question(不是答案)中的初始問題實現。 我還添加了

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

來設置urlrewrite.xml文件中的特定路徑。 在項目中,我把文件放在src/main/webapp中。

這種方式在使用'mvn spring-boot:run'時會起作用,我認爲它滿足您在jar中運行的需要。在我的項目中,我建立了一個戰爭檔案,這也是可行的。

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(), "@@[email protected]@"); 
      checkConf(conf); 
     } catch (IOException ex) { 
      throw new ServletException("Unable to load URL rewrite configuration file from " + CONFIG_LOCATION, ex); 
     } 
    } 
} 
-1

下面的代碼爲我工作。

請使用下面的依賴關係:

  <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(); 
} 
} 
+0

任何理由將其標記爲一個否定的回答信息? –

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); 
相關問題