如何在Rest-Assured java中使用证书进行HTTPS GET调用

roh*_*m19 14 java certificate rest-assured

如何使用Java中的Rest-Assured向需要证书的端点进行GET调用.我有证书作为.pem格式.在PEM文件中有证书和私钥.

Sae*_*fam 18

在我的情况下,使用"轻松的HTTPs验证"解决了我的问题:

given().relaxedHTTPSValidation().when().post("https://my_server.com")
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,这只是禁用了SSL检查,因此它既不发送客户端证书也不检查远程服务器的证书。 (2认同)

roh*_*m19 11

得到它使用以下代码 -

KeyStore keyStore = null;
SSLConfig config = null;

try {
        keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(
                new FileInputStream("certs/client_cert_and_private.p12"),
                password.toCharArray());

    } catch (Exception ex) {
        System.out.println("Error while loading keystore >>>>>>>>>");
        ex.printStackTrace();
    }

    if (keyStore != null) {

        org.apache.http.conn.ssl.SSLSocketFactory clientAuthFactory = new org.apache.http.conn.ssl.SSLSocketFactory(keyStore, password);

        // set the config in rest assured
        config = new SSLConfig().with().sslSocketFactory(clientAuthFactory).and().allowAllHostnames();

RestAssured.config = RestAssured.config().sslConfig(config);
RestAssured.given().when().get("/path").then();
Run Code Online (Sandbox Code Playgroud)