복붙노트

[SPRING] 스프링 부트와 함께 실행 가능한 jar 명령을 사용할 때 Resource FileNotFoundException

SPRING

스프링 부트와 함께 실행 가능한 jar 명령을 사용할 때 Resource FileNotFoundException

내 스프링 부팅 응용 프로그램에서 https를 사용하려면 다음을 수행했습니다.

@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를 사용하여 실행할 경우 : 예상대로 작동합니다.

하지만 java -jar target / xxx-xxx-service-0.1.17-SNAPSHOT.jar을 사용하여 실행 가능한 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. ==============================

    1.위의 주석과이 코드는 나를 도왔습니다 ..

    위의 주석과이 코드는 나를 도왔습니다 ..

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

  2. from https://stackoverflow.com/questions/37713335/resource-filenotfoundexception-when-using-executable-jar-command-with-spring-boo by cc-by-sa and MIT license