如何在放心的java中使用证书进行HTTPS GET调用

2022-09-02 02:45:53

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


答案 1

在我的情况下,使用“宽松的HTTP验证”解决了我的问题:

given().relaxedHTTPSValidation().when().post("https://my_server.com")

答案 2

让它使用以下代码 -

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();

推荐