Mongocxx X509身份验证错误-SSLHandshakeFailed“,” errmsg“:”对等方未提供SSL证书;连接被拒绝”

问题描述

我正在尝试使用x509Authentication连接到mongoDb。我已经生成了自签名证书。 即使我在代码中提供了证书的确切路径,仍然在mongodb服务器中出现以下错误

{“ t”:{“ $ date”:“ 2020-09-14T15:14:20.108 + 05:30”},“ s”:“ I”,“ c”:“ NETWORK”,“ id” :22944,“ ctx”:“ conn5”,“ msg”:“连接已结束”,“ attr”:{“远程”:“ 192.168.43.20:62215”,“ connectionCount”:1}} {“ t”:{“ $ date”:“ 2020-09-14T15:14:47.154 + 05:30”},“ s”:“ I”,“ c”:“ NETWORK”,“ id”:22944, “ ctx”:“ conn4”,“ msg”:“连接结束”,“ attr”:{“远程”:“ 192.168.43.20:62187”,“ connectionCount”:0}} {“ t”:{“ $ date”:“ 2020-09-14T17:03:04.641 + 05:30”},“ s”:“ I”,“ c”:“ NETWORK”,“ id”:22943, “ ctx”:“侦听器”,“ msg”:“连接已接受”,“ attr”:{“远程”:“ 192.168.43.20:62813”,“ sessionId”:6,“ connectionCount”:1}} {“ t”:{“ $ date”:“ 2020-09-14T17:03:04.720 + 05:30”},“ s”:“ E”,“ c”:“ NETWORK”,“ id”:23280, “ ctx”:“ conn6”,“ msg”:“对等方未提供SSL证书;连接被拒绝”} {“ t”:{“ $ date”:“ 2020-09-14T17:03:04.720 + 05:30”},“ s”:“ I”,“ c”:“ NETWORK”,“ id”:22988, “ ctx”:“ conn6”,“ msg”:“从客户端接收请求时出错。来自远程的结束连接”,“ attr”:{“ error”:{“ code”:141,“ codeName”:“ SSLHandshakeFailed”,“ errmsg“:”对等方未提供SSL证书;连接被拒绝“},”远程“:” 192.168.43.20:62813“,” connectionId“:6}} {“ t”:{“ $ date”:“ 2020-09-14T17:03:04.721 + 05:30”},“ s”:“ I”,“ c”:“ NETWORK”,“ id”:22944, “ ctx”:“ conn6”,“ msg”:“连接已结束”,“ attr”:{“远程”:“ 192.168.43.20:62813”,“ connectionCount”:0}}

我的代码:-

std :: string ca_file =“ D:/Certs/my.crt”; std :: string pem_file =“ D:/Certs/user.pem”;

/*if (FILE *file = fopen(ca_file.c_str(),"r")) {
    fclose(file);
}
if (FILE *file = fopen(pem_file.c_str(),"r")) {
    fclose(file);
}*/
std::fstream filestream;
filestream.open(ca_file);
if (filestream.fail())
{
    std::cout << "certificate not passed";
}
else 
{
    std::cout << "certificate passed"<<std::endl;
}



tls_options.ca_file(ca_file);
tls_options.ca_dir("D:/Certs/");
tls_options.pem_file(pem_file);
tls_options.pem_password("1234");
tls_options.allow_invalid_certificates(true);
client_options.tls_opts(tls_options);
m_cpp_client = NULL;

try
{
    mongocxx::instance inst{};
    mongocxx::client conn{ mongocxx::uri{"mongodb://192.168.43.20:27017/?authMechanism=MONGODB-X509&tls=true"},client_options};
    // auto collection = conn["test"]["restaurants"];
    mongocxx::database db = conn["admin"];
    auto cursor1 = db.list_collections();
    for (const bsoncxx::document::view& doc : cursor1)
    {
        bsoncxx::document::element ele = doc["name"];
        std::string name = ele.get_utf8().value.to_string();
        std::cout << name << std::endl;

    }
}

请让我知道是否可以帮助我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)