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

14,266

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?

Share:
14,266
David Sonnenfeld
Author by

David Sonnenfeld

Updated on June 04, 2022

Comments

  • David Sonnenfeld
    David Sonnenfeld almost 2 years

    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

  • vegemite4me
    vegemite4me over 8 years
    This will only work in your local development environment. Once you build your artefact, the path src/main/resources will be invalid.
  • Michel Jung
    Michel Jung almost 8 years
    Please update your original question instead of posting a new question as an answer
  • csikos.balint
    csikos.balint about 7 years
    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!