java - 如何在java中进行相互SSL认证

标签 java ssl mutual-authentication

我想使用java代码进行相互SSL身份验证,但我没有成功我有key.pemcert.pem文件来进行身份验证我已经尝试过与 Curl 一起使用

curl -X POST -d '{ "Channel": "....}' -H "Content-Type: application/json"-H "Auth1: ***"-H "Auth2: ***"-k https://******/webservices/JSON/Default.aspx --cert "cert.pem"--key "Key.pem"

并且工作正常,然后我尝试创建 java 程序引用 Send https request in java using .pem file 但服务器返回“CERT_MISSING”。我也尝试过这个https://www.naschenweng.info/2018/02/01/java-mutual-ssl-authentication-2-way-ssl-authentication/此代码创建 p12、crt 和 jks 文件,如上面的链接所示,但仍然收到相同的错误“CERT_MISSING”。 这是 NodeJS 的工作示例:

var https = require("https");
var fs = require("fs");

var jsonData = {
    "Channel": ....
}

var options = {
    hostname: "****",
    port: 443,
    path: '/webservices/JSON/Default.aspx',
    method: 'POST',
    timeout: this.TimeOut,
    headers: {'Content-Type':'application/json',"Auth1": "****","Auth2": "*****"},
    json: true,
    key: fs.readFileSync('Key.pem'),
    cert: fs.readFileSync('cert.pem')
}

var req = https.request(options, function(res) {
    res.on('data', function(data) {
        var response = JSON.parse(data)
            console.log(response)

            req.end();
    });
});
req.on('error', function(e) {
    console.log("ERROR:");

    })


req.write(JSON.stringify(jsonData));
req.end();

请帮助我。

最佳答案

我认为您需要“internediate-cert”文件将所有证书连接到一个PEM文件中,例如cat“internediate-cert.pem”“codika_cert.pem”“Key.pem”> full-chain.keycert。质子交换膜 然后使用别名和密码生成 PKCS12(.p12) keystore ,如 pkcs12 -export -in "full-chain.keycert.pem"-out full-chain.keycert.p12 -name alias -noiter -nomaciter 然后使用 full-chain.keycert.p12 作为带有密码的 KeyStore。它应该有效。

关于java - 如何在java中进行相互SSL认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60465308/

相关文章:

java - 除了java反射api之外,是否有更好的方法来获取对象的字段 getter ,或者我滥用了PropertyDescriptor的getReadMethod?

iphone - 出于开发目的,如何使用 iOS 7 的 NSURLSession 及其委托(delegate)方法系列接受自签名 SSL 证书?

ubuntu - 建议删除 SSL 证书并在 ubuntu 16 上使用 Letsencrypt NGINX 生成一个

ssl - 使用 Heroku 获取相互 SSL 身份验证信息

tomcat - Apache tomcat 相互认证自签名证书 REST 服务

java - AKKA 消息超时

java - ListView 未正确填充/不可见

go - 从 golang 中传入的 https 请求中提取公用名

java - 您如何看待一种算法优于另一种算法的地方

ruby-on-rails - 在 Windows 上使用 Cygwin 安装 RVM - Curl 抛出状态 23