title: How to use LUNA HSM with Java
body: <p>I am working on a spring boot application which needs to  communicate with another service through HTTPS connection using TLS certificates.
  Earlier the requirements were not clear and I started using the Java Keystore/Truststore for the certificats and key pairs.
  But later we decided to migrate to HSM Luna Client where we need to generate the certificates and public/private keys.</p>
  <p>I have gone through the Luna documentation from Thales website on how we can generate the key pairs and csr files.</p>
  <p><a href="https://thalesdocs.com/gphsm/luna/7.2/docs/network/Content/sdk/java/java_keytool_with_luna_hsm.htm" rel="nofollow noreferrer">https://thalesdocs.com/gphsm/luna/7.2/docs/network/Content/sdk/java/java_keytool_with_luna_hsm.htm</a></p>
  <p>But I do not understand How i can use these certificate in my java application for SSL/TLS connection.</p>
  <p>Do I have to now use the JSP/JCPROV libraries. Or I can somehow add the keystore/truststore files in my java application classpath just like i did earlier.
  The documentation does not depict any sample code on using the keystore/truststore.</p>
  <p>I just want to make  HTTPS calls to another service using RestTemplate and recieve the response in my JAVA application.</p>
  <p>Can someone please help me understand this. It would be better if someone can provide the code snippets as well.</p>
  <p>I am posting my code below:</p>
  <pre><code>@Configuration
  Class ApplicationConfig{
     
    static {
     com.safenetinc.luna.LunaSlotManager.getInstance().login(&quot;Partition-password&quot;);
     java.security.Provider provider = new com.safenetinc.luna.provider.LunaProvider();
     java.security.Security.removeProvider(provider.getName());
     java.security.Security.insertProviderAt(provider,3)
    
   } 
    public ClientHttpRequestFactory requestFactory(){
    KeySTore keystore = KeyStore,getInstance(&quot;Luna&quot;);
    InputStream inputStream = new FileInputStream(&quot;path/to/keystore&quot;);
    keystore.load(inputStream,&quot;password&quot;.toCharArray());
  
    SSLContext sslContext = SSLContextBuilder.create()
                         .loadKeyMaterial(keystore, 
   &quot;password&quot;.toCharArray())
                         .loadTrustMaterial(&quot;pathTocacert&quot;, 
    &quot;password&quot;.toCharArray())
                          .setProtocol(&quot;TLSv1.2&quot;)
                          .build();
  
     }
     @Bean
      public RestTemplate restTemplate(){
        return new RestTemplate(requestFactory());
       }
  }
  </code></pre>
  <p>I am not sure this code is going to work. I have done a lot of google and then found how we can create a SSL context with Luna client.
  If someone has done it before Please provide some guidance.</p>