2016-06-08 36 views
0

要在我的spring-boot應用中啓用https,我做了以下操作。在彈簧引導中使用可執行jar命令時發生Resource FileNotFoundException

@Bean 
@Inject 
public EmbeddedServletContainerCustomizer containerCustomizer() throws FileNotFoundException 
{ 
    final String absoluteKeystoreFile = ZenoTuringServiceApp.class.getClassLoader().getResource("test.jks").getFile(); 

    return (ConfigurableEmbeddedServletContainer factory) -> { 
     TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory; 
     containerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) (Connector connector) -> { 
      connector.setSecure(true); 
      connector.setScheme("https"); 
      connector.setAttribute("keystoreFile", absoluteKeystoreFile); 
      connector.setAttribute("keystorePass", "test"); 
      connector.setAttribute("keystoreType", "JKS"); 
      connector.setAttribute("clientAuth", "false"); 
      connector.setAttribute("sslProtocol", "TLS"); 
      connector.setAttribute("SSLEnabled", true); 
     }); 
    }; 
} 

如果我把它用MVN彈簧啓動時運行:運行它按預期工作。

但是,當我運行使用可執行jar與java -jar target/xxx-xxx-service-0.1.17-SNAPSHOT.jar我得到FileNotFoundException。

Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat"; Protocol handler start failed 
     at org.apache.catalina.connector.Connector.startInternal(Connector.java:993) 
     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
     ... 18 common frames omitted 

Caused by: java.io.FileNotFoundException: /private/var/folders/wl/xxx77_523z44yjdgx2y7xxxc217h/T/tomcat.8061417798873093914.8091/file:/Users/xxx/Work/Himalay/xxx/xxx-xxx-service/target/xxx-xxx-service-0.1.17-SNAPSHOT.jar!/test.jks (No such file or directory) 
     at java.io.FileInputStream.open0(Native Method) 
     at java.io.FileInputStream.open(FileInputStream.java:195) 
     at java.io.FileInputStream.<init>(FileInputStream.java:138) 
     at org.apache.tomcat.util.net.jsse.JSSESocketFactory.getStore(JSSESocketFactory.java:433) 
     at org.apache.tomcat.util.net.jsse.JSSESocketFactory.getKeystore(JSSESocketFactory.java:339) 
     at org.apache.tomcat.util.net.jsse.JSSESocketFactory.getKeyManagers(JSSESocketFactory.java:597) 
+1

不能引用資源(JAR內)作爲'File'。你**必須**使用InputStream。所以'keystoreFile'應該可能位於你的JAR之外,因爲顯然Tomcat希望有一個到'File'的路徑。 – Tunaki

+0

由於您在Tomcat之外執行此操作,因此您沒有包含「web」罐。您需要包含來自Tomcat的Java EE庫。 – Grayson

+2

這可能會有所幫助https://kamwo.me/java-load-file-from-classpath-in-spring-boot/ – austin

回答

0

上述評論和這個代碼幫我..

// support loading the JKS from the classpath (to get around Tomcat limitation) 
private static File getTuringKeyStoreFile() throws IOException { 
    ClassPathResource resource = new ClassPathResource("test.jks"); 

    // Tomcat won't allow reading File from classpath so read as InputStream into temp File 
    File jks = File.createTempFile("ssl_keystore", ".jks"); 
    InputStream inputStream = resource.getInputStream(); 
    try { 
     FileUtils.copyInputStreamToFile(inputStream, jks); 
    } finally { 
     IOUtils.closeQuietly(inputStream); 
    } 

    return jks; 
} 

參考:https://github.com/robinhowlett/everything-ssl