I need to send https request with REST assured having client .crt certificate and public key .key How do I send request if my certificate and key in project like
"src/test/resources/certificate.crt" "src/test/resources/key.key"
Advertisement
Answer
String clientCertificatePath = "certs/ClientCertificate.p12"; String trustStorePath = "C:/Program Files/Java/jre1.8.0_91/lib/security/cacerts"; String trustStorePassword = "changeit"; // default trust store password KeyStore clientStore = KeyStore.getInstance("PKCS12"); clientStore.load(new FileInputStream(clientCertificatePath), clientPassword.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(clientStore, clientPassword.toCharArray()); KeyManager[] kms = kmf.getKeyManagers(); KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(new FileInputStream(trustStorePath), trustStorePassword.toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trustStore); TrustManager[] tms = tmf.getTrustManagers(); SSLContext sslContext = null; sslContext = SSLContext.getInstance("TLS"); sslContext.init(kms, tms, new SecureRandom()); SSLSocketFactory lSchemeSocketFactory=null; lSchemeSocketFactory = new SSLSocketFactory(clientStore, clientPassword, trustStore); // configure Rest Assured RestAssured.config = RestAssured.config().sslConfig(sslConfig().with().sslSocketFactory(lSchemeSocketFactory).and().allowAllHostnames());