![](/img/trans.png)
[英]Issues authenticating a post request with a certificate and private key in Kotlin / Java
[英]Java - Using a certificate and private key to make an HTTP POST request
我正在用 Java 發出 POST 請求,該請求需要 ssl 證書和私鑰組合。 我查看了 Java KeyStore 並使用以下兩個命令從 .key 和 .cert 文件創建了一個 .jks 文件:
winpty openssl pkcs12 -export -in certificate.crt -inkey privatekey.key -out abc.p12
keytool -importkeystore -srckeystore abc.p12 -srcstoretype PKCS12 -destkeystore abc.jks -deststoretype JKS
但這使請求失敗,並出現 403 異常。 實際上,我想在 java 中執行以下 Javascript 的功能:
function cardBalance(intent, session, response) {
var options = {
key: fs.readFileSync('privatekey.key'),
cert: fs.readFileSync('certificate.crt'),
host: "blah.blah.blah.com',
path: '/abc/def/ghi/jkl/mno/pqr/creditcardsummary',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + session.user.accessToken,
'Content-Type': 'application/json',
'Version': '1.1.0',
'zId': '1234',
'aId': '123456789',
'bId': 'bId',
'AppName': 'AppName'
}
};
var postData = JSON.stringify({
'acctnum': '00002600452999820832'
});
console.log(options);
var req = https.request(options, function (res) {
console.log('Request Credit Card Balance');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data);
console.log(data);
if (data.CreditCardSummary.status === 'SUCCESS') {
var balance = data.balance;
console.log('Balance: ' + balance);
var speechOutput = 'The current balance is $' + balance;
response.tell(speechOutput);
} else {
response.tell('There was a problem getting your card balance.');
}
});
});
req.on('error', function (e) {
console.log('An Error Occurred when calling the Gateway. ' + e);
response.tell(e);
});
req.write(postData);
req.end();
}
我想通了,我想為其他人提供參考。 我必須將我的 .key 和 .crt 文件轉換為 .p12 文件,然后將其轉換為 .jks 文件。 我通過點擊這里的鏈接在 Java 密鑰庫中導入現有的 x509 證書和私鑰以在 ssl 中使用,一旦我正確設置了我的密鑰庫,我就按照 Java 如何請求需要客戶端證書進行身份驗證的 URL頁面進行操作。 我出錯的地方是我沒有正確設置我的 JVM 參數。 我不得不把這些javax.net.ssl.keyStore和javax.net.ssl.keyStorePassword 。 一旦我將它添加到我的 JVM 中,它就會完美運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.