user565
user565

Reputation: 1001

How to set KeyStore and trustStore path within a jar

I have keystore and truststore files which i want to keep inside a executable jar. In IDE i put these file in a /main/resources/ and was getting a path from there by using ClassLoader, But within a jar file can only be load as a stream. Is there any possibility to get a path of these file as i need to create it SslContextFactory which only get a absolute path rather than a stream?

Upvotes: 4

Views: 1846

Answers (2)

Oleg
Oleg

Reputation: 6314

Haven't tried it but I think you can do:

SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStoreResource(Resource.newClassPathResource("something.jks"));

Upvotes: 0

user565
user565

Reputation: 1001

I think there is no other way rather than create a temp file and delete deleteOnExit() using commons-io.

public static File stream2file(InputStream in, String PREFIX) throws IOException {
    final File tempFile = File.createTempFile(PREFIX, ".jks");
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }
    return tempFile;
}

Upvotes: 1

Related Questions