-2

I need to call soap web services from java so i'm using ".p12" file for authentication. I'm using the same file in soap ui there it is working fine but in java it is giving SSL error.. how to link p12 file for authentication using ssl from java..

 public static void setUp() {
        System.setProperty("javax.net.ssl.keyStore", "ex.p12");
        System.setProperty("javax.net.ssl.keyStorePassword", "password");
    }   

   private static void initSSLFactories() {
        final String KEYSTOREPATH = "ex.p12";
        final char[] KEYSTOREPASS = "ff".toCharArray();
        final char[] KEYPASS = "ff".toCharArray();
//ssl config
        try (InputStream storeStream = FirstTest.class.getResourceAsStream(KEYSTOREPATH)) {
            setSSLFactories(storeStream, "PKCS12", KEYSTOREPASS, KEYPASS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setSSLFactories(InputStream keyStream, String keystoreType, char[] keyStorePassword, char[] keyPassword) throws Exception
    {
        KeyStore keyStore = KeyStore.getInstance(keystoreType);

        keyStore.load(keyStream, keyStorePassword);

        KeyManagerFactory keyFactory =
                KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

        keyFactory.init(keyStore, keyPassword);

        KeyManager[] keyManagers = keyFactory.getKeyManagers();

        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(keyManagers, null, null);
        SSLContext.setDefault(sslContext);
    }

1 Answers1

0

You can create a client something like this:

    public Client getClient() {

        SslConfigurator sslConfig = SslConfigurator
                .newInstance()
                .trustStoreFile(TRUST_STORE_FILE_PATH) //The key-store file where the certificate is saved.
                .trustStorePassword(TRUST_STORE_PASSWORD_PATH);//password of the key-store file.

        SSLContext sslContext = sslConfig.createSSLContext();

        Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();

        return client;
    }
Diabolus
  • 268
  • 2
  • 15