在春季启动中使用可执行jar命令时出现资源FileNotFoundException [英] Resource FileNotFoundException when using executable jar command with spring boot

查看:246
本文介绍了在春季启动中使用可执行jar命令时出现资源FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在我的春季启动应用中启用 https ,我执行了以下操作.

To enable https in my spring-boot app I did the following.

@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 spring-boot:run 运行它,它将按预期运行.

If I run it using mvn spring-boot:run it works as expected.

但是当我将可执行jar与java -jar target/xxx-xxx-service-0.1.17-SNAPSHOT.jar一起使用时,我得到FileNotFoundException.

But when I run using the executable jar with java -jar target/xxx-xxx-service-0.1.17-SNAPSHOT.jar I get the 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)

推荐答案

上面的注释和此代码对我有所帮助.

The comments above and this code helped me..

// 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

这篇关于在春季启动中使用可执行jar命令时出现资源FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆