How to load a keystore, which is inside the resource folder (maven)?
Asked Answered
S

1

8

I have a custom SSL factory, where I load my own truststore.

Now when I put the truststore.jks file into the project root folder, it works with the following line:

ks.load(new FileInputStream("/truststore.jks", passphrase);

But I want my truststore inside my resource folder, which was built with maven where the path is src/main/resources.

Then I do and it doesn't work with the following line:

ks.load(this.getClass().getResourcesAsStream("/truststore.jks"), passphrase);

Though the input stream exists. I checked it. It only fails when I do ks.load(...).

The exception that I get is:

 java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

Why is that?

Regards, Dave

Smelser answered 20/2, 2013 at 22:0 Comment(2)
one thing you could try comes to mind. Specify "/truststore.jks" in your getResourceAsStream so it searches from the root of the classpath.Novelist
I tried that too, with no luck...Smelser
S
-5

Strange, it works now...

I had

Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", "/truststore.jks");
systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");
System.setProperties(systemProps);

and change the second line to

systemProps.put( "javax.net.ssl.trustStore", "src/main/resources/truststore.jks");

Anyone knows why? Is this solution ok?

Smelser answered 21/2, 2013 at 0:14 Comment(3)
This will only work in your local development environment. Once you build your artefact, the path src/main/resources will be invalid.Geoponics
Please update your original question instead of posting a new question as an answerSawfly
This solution has nothing to do with the fact that the truststore is a resource. Your answer (as noted before me) only works during development, so I does not work!Wentletrap

© 2022 - 2024 — McMap. All rights reserved.