SSL连接证书麻烦,请放心

发布于 2025-02-11 14:16:37 字数 1976 浏览 2 评论 0原文

有一个问题,我已经战斗了几天 - 如何通过放心来建立SSL连接?这是我的尝试,但是有了此代码:

public class ApiTests {
    @BeforeAll
    static void setUp() {
        RestAssured.baseURI = "my host";
    }

    @Test
    void createUserTest() throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        String keyStorePass = "my pass";
        try (FileInputStream fis = new FileInputStream("D:\\Idea_projects\\TestApi\\src\\test\\resources\\my_cert.p12")) {
            keyStore.load(fis, keyStorePass.toCharArray());
        }

        given()
                .keyStore(keyStore)
                .trustStore("C:\\Program Files\\Java\\jdk-11.0.13\\lib\\security\\cacerts", "changeit")
                .contentType(JSON)
                .when()
                .post("users")
                .then()
                .statusCode(200)
                .body("name", is("Valentin"))
                .body("job", is("qa"));
    }
}

抛出例外:

No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)
groovy.lang.MissingMethodException: No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)

我尝试使用sparesedhttpsvalidation(),但它说:

Received fatal alert: bad_certificate
javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate

即,据我了解,我仍然需要以某种方式添加证书,但是如何呢?我无法想象。 如果有人可以告诉我问题是什么或我的代码中有什么问题,请帮助:(

there is a problem, I have been fighting for several days - how to establish an SSL connection via Rest Assured? Here are my attempts, but with this code:

public class ApiTests {
    @BeforeAll
    static void setUp() {
        RestAssured.baseURI = "my host";
    }

    @Test
    void createUserTest() throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        String keyStorePass = "my pass";
        try (FileInputStream fis = new FileInputStream("D:\\Idea_projects\\TestApi\\src\\test\\resources\\my_cert.p12")) {
            keyStore.load(fis, keyStorePass.toCharArray());
        }

        given()
                .keyStore(keyStore)
                .trustStore("C:\\Program Files\\Java\\jdk-11.0.13\\lib\\security\\cacerts", "changeit")
                .contentType(JSON)
                .when()
                .post("users")
                .then()
                .statusCode(200)
                .body("name", is("Valentin"))
                .body("job", is("qa"));
    }
}

Throws exception:

No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)
groovy.lang.MissingMethodException: No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)

I tried to use relaxedHTTPSValidation(), but it says:

Received fatal alert: bad_certificate
javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate

I.e. as I understand it, I still need to add a certificate somehow, but how? I can't imagine already.
If someone can tell me what the problem is or what is wrong in my code, please help :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2025-02-18 14:16:37

如果突然有人面对类似的问题,那么这就是解决方案。
您必须有2个证书:服务器和客户端。
然后,将服务器放在Java的Cacerts存储库中,并使用命令:

KEYTOOL -IMPORTCERT -STORETYPE PKCS12 -Keystore Truststore.p12
-StorePass密码-Alias mpzca -file root.crt -noprompt

,然后在代码中执行以下操作:

public class DealerProtocolTests {
    
        @Test
        void sendDealerRequest() throws Exception {
            RestAssured.config = RestAssured.config().sslConfig(
                    new SSLConfig()
                            .trustStore("src/test/resources/certificate/SERVER.p12", "password")
                            .keyStore("src/test/resources/certificate/CLIENT.p12", "password"));
            
            Response response = null;
             response =
                    given()
                            .config(config)
                            .spec(xmlSpec)
                            .basePath("/xmlInteface")
                            .body("")
                            .when()
                            .get()
                            .then()
                            .statusCode(200)
                            .log().all()
                            .extract().response();
    
            System.out.println(response.prettyPrint());
        }
    }

If suddenly someone is faced with a similar problem, then here is the solution.
You must have 2 certificates: server and client.
Then you put the server one in the cacerts repository in Java with the command:

keytool -importcert -storetype PKCS12 -keystore trustStore.p12
-storepass password -alias mpzCA -file root.crt -noprompt

Then, in the code, do as below:

public class DealerProtocolTests {
    
        @Test
        void sendDealerRequest() throws Exception {
            RestAssured.config = RestAssured.config().sslConfig(
                    new SSLConfig()
                            .trustStore("src/test/resources/certificate/SERVER.p12", "password")
                            .keyStore("src/test/resources/certificate/CLIENT.p12", "password"));
            
            Response response = null;
             response =
                    given()
                            .config(config)
                            .spec(xmlSpec)
                            .basePath("/xmlInteface")
                            .body("")
                            .when()
                            .get()
                            .then()
                            .statusCode(200)
                            .log().all()
                            .extract().response();
    
            System.out.println(response.prettyPrint());
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文