Advertisement
Guest User

java mutual authentification

a guest
Aug 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1.             // -- keystore
  2.             KeyStore ks = KeyStore.getInstance("PKCS12");
  3.             ks.load(new FileInputStream(KEYSTORE), KEYSTORE_PASS.toCharArray());
  4.             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  5.             kmf.init(ks, KEYSTORE_PASS.toCharArray());
  6.  
  7.             // -- trustore
  8.             KeyStore ts = KeyStore.getInstance("JKS");
  9.             ts.load(new FileInputStream(TRUSTORE), TRUSTORE_PASS.toCharArray());
  10.             TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  11.             tmf.init(ts);
  12.  
  13.             // -- kontext
  14.             SSLContext sslContext = SSLContext.getInstance("TLSv1");
  15.             sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
  16.  
  17.             // -- factory
  18.             SSLSocketFactory factory = sslContext.getSocketFactory();
  19.             SSLSocket sslSocket = (SSLSocket) factory.createSocket(HOST, PORT);
  20.             sslSocket.startHandshake();
  21.  
  22.             // -- connection
  23.             HttpsURLConnection connection = (HttpsURLConnection) new URL(ENDPOINT).openConnection();
  24.             connection.setRequestMethod("GET");
  25.             connection.setRequestProperty("Content-Type", "application/xml");
  26.             connection.setRequestProperty("Content-Language", "en-US");
  27.             connection.setSSLSocketFactory(factory);
  28.  
  29.             // -- processing request/response
  30.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  31.             String line;
  32.             StringBuilder lines = new StringBuilder();
  33.             while ((line = bufferedReader.readLine()) != null) {
  34.                 lines.append(line).append(LINE_BREAKER);
  35.             }
  36.             System.out.println("response from " + HOST + ":" + LINE_BREAKER + lines);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement