Deo*_*eia 2 ssl client-certificates ssl-certificate rust hyper
我有一个在 Bluemix 中使用 Docker 容器部署微服务的项目。所有微服务都是用 Java 编写的,通信使用 JKS 文件。
我还在 Node.js 中使用 Express.js 开发了一个微服务。为了使用其他微服务,我使用了带有option.agentOptionsfeature 和 a的 Request 模块pfx file,如下所示:
var options = {
uri: config.get("https://www.example.com/ms/service"),
method: 'POST',
body: data,
json: true,
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
agentOptions: {
pfx: fs.readFileSync(config.get("/path/to/file.pfx")),
passphrase: config.get("passphraseText"),
servername: config.get("serverName")
}
};
request(options, function (error, response, data) {
//handing response
});
Run Code Online (Sandbox Code Playgroud)
我尝试将 Solicit crate与HTTPS 的默认示例一起使用,但它失败了:
var options = {
uri: config.get("https://www.example.com/ms/service"),
method: 'POST',
body: data,
json: true,
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
agentOptions: {
pfx: fs.readFileSync(config.get("/path/to/file.pfx")),
passphrase: config.get("passphraseText"),
servername: config.get("serverName")
}
};
request(options, function (error, response, data) {
//handing response
});
Run Code Online (Sandbox Code Playgroud)
我找不到其他的板条箱、库或框架来制作它,我该如何提出这个请求?
编辑
显然,Solicit 不是缺乏维护的替代方案,因此它不再是该问题的替代解决方案,原因如下。
目前,您应该更喜欢hyper客户端而不是solicit. 后者自 2015 年以来一直没有更新,并且hyper正在得到更好的维护。将hyper = "0.10.10", 和添加hyper-native-tls = "0.2.2"到您的依赖项中。为了指定要使用的客户端证书,我们可以利用native_tls. 特别是,TlsConnectorBuilder并且Pkcs12是您正在寻找的。
use std::fs::File;
use std::io::Read;
use hyper::client::Client;
use hyper::net::HttpsConnector;
use hyper_native_tls::NativeTlsClient;
use hyper_native_tls::native_tls::{TlsConnector, Pkcs12};
// fetch the PKCS12 client certificate
let cert = {
let cert_file = File::open("/path/to/cert.pfx")?;
let mut cert_raw = Vec::new();
cert_file.read_to_end(&mut cert_raw)?;
Pkcs12::from_der(&cert_raw, "mypassword")?
};
// specify the TLS connection with the builder pattern
let tls_conn = TlsConnector::builder()
.identity(cert)?
.build()?;
let ssl = NativeTlsClient::from(tls_conn)?;
let https_conn = HttpsConnector::new(ssl);
// proceed as usual
let client = Client::with_connector(https_conn);
let endpoint = "https://www.example.com/ms/service");
let resp = client.get(endpoint).send()?;
Run Code Online (Sandbox Code Playgroud)
在 中solicit,文档指出tls子模块仅在为此依赖项启用“tls”功能时才可用。然而,这会导致进一步的依赖冲突(请参阅为什么即使我的 Cargo.toml 中有 openssl 0.7.14,slice 0.4.4 也会尝试使用 openssl 0.9.12?)。坚持hyper而不是solicit是一个更安全的选择。
| 归档时间: |
|
| 查看次数: |
2946 次 |
| 最近记录: |