user2831852
user2831852

Reputation: 615

Cacerts file path to javax.net.ssl.trustStore does not work in .war

I need to set the path of a cacerts file to the trustStore, to do this I did:

String keystore = "src/main/resources/cacerts";
System.setProperty("javax.net.ssl.trustStore", keystore);

I want to run the project through the STS IDE (Spring boot) it works, but when I generate the .war file and run it from the error because it does not find the path to the cacerts file, how can I set the file to work with .WAR?

I also tried to remove the System.setProperty and set the cacerts at the time of running java -jar but it also did not work:

java -Djavax.net.ssl.trustStore=./cacerts -jar target/uaa-0.0.1-SNAPSHOT.war

Upvotes: 0

Views: 1459

Answers (1)

VGR
VGR

Reputation: 44414

A .war file is a single compressed archive; its contents are not files and cannot be directly referenced in the javax.net.ssl.trustStore property.

You can, however, use Class.getResourceAsStream to copy the certificates to a new file:

Path keystore = Files.createTempFile(null, null);
try (InputStream stream = getClass().getResourceAsStream("/cacerts")) {
    Files.copy(stream, keystore, StandardCopyOption.REPLACE_EXISTING);
}

System.setProperty("javax.net.ssl.trustStore", keystore.toString());

Upvotes: 1

Related Questions