1 回答
TA贡献1805条经验 获得超9个赞
我找到了根据需要自定义 SSL 配置的方法。附有稍微审查的代码。寻找“圣杯”评论:
protected static RequestSpecification configureRestAssured() {
// Create the ReqSpec instance:
RequestSpecification reqSpecToBuild = new RequestSpecBuilder().build();
// Configure more simple stuff for common request specification:
reqSpecToBuild
.header("Content-Type", "application/json")
.baseUri(envConf.getBaseURI())
.config(getRAconfig());
return reqSpecToBuild;
}
// Add extended config object to the request spec:
private static RestAssuredConfig getRAconfig() {
// Create a special socket with our keystore and ALLOW_ALL_HOSTNAME_VERIFIER:
SSLSocketFactory sslSocket = getSSLsocket (envConf.getKeystoreFile(), somePass, somePass);
// Create a configuration instance to load into the request spec via config():
RestAssuredConfig raConfigToBuild = RestAssuredConfig.config()
// Set SSL configuration into the RA configuration, with an SSLConfig object, that refers to our socket:
.sslConfig(SSLConfig.sslConfig().sslSocketFactory(sslSocket));
return raConfigToBuild;
}
private static SSLSocketFactory getSSLsocket(String ksPath, String ksPassword, String pkPassword) {
KeyStore keystore = KeyStore.getInstance("PKCS12");
// Load keystore file and password:
keystore.load(new FileInputStream(ksPath), ksPassword.toCharArray());
SSLContext context = SSLContexts.custom()
.loadKeyMaterial(keystore, pkPassword.toCharArray())
.build();
// This is the holy grail:
SSLSocketFactory sslSocketToBuild = new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return sslSocketToBuild;
}
}
请注意,我不仅向 SSLSocketFactory 的构造函数提供一个参数,而且还提供常规参数(上下文)以及 ALLOW_ALL_HOSTNAME_VERIFIER 参数 - 这会产生影响!
添加回答
举报