SSL连接证书麻烦,请放心
有一个问题,我已经战斗了几天 - 如何通过放心来建立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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果突然有人面对类似的问题,那么这就是解决方案。
您必须有2个证书:服务器和客户端。
然后,将服务器放在Java的Cacerts存储库中,并使用命令:
,然后在代码中执行以下操作:
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:
Then, in the code, do as below: