在 Windows 上的 Microsoft cpprestsdk 中使用 http_listener 的 TLS

TLS with http_listener in Microsoft cpprestsdk on Windows

我正在尝试使用 cpprestsdk (casablanca) 配置 TLS。我读过的所有文档都说这仅在 Windows 上受支持,但是当我查看与配置 SSL 相关的任何代码时,我看到它周围有 #if !defined _WIN32。我无法配置任何证书,因为这些功能对我是隐藏的。有人知道这里发生了什么吗?

对于Windows,你只需要在url中将协议从'http'更改为'https',前提是你已经将SSL服务器证书附加到你的服务器端口正在服务器 运行 的机器上使用。有关此过程中涉及的非常好的描述和详细步骤,请参阅 here

对于Linux,有web::http::experimental::listener::http_listener_config。您可以设置 SSL 选项,例如使用 conf 对象提供证书、私钥、链以及其他选项,然后将其提供给 http_listener 对象。

web::http::experimental::listener::http_listener_config             conf;
conf.set_ssl_context_callback([](boost::asio::ssl::context &ctx)
{
    ctx.set_options(boost::asio::ssl::context::default_workarounds);

    // Password callback needs to be set before setting cert and key.
    ctx.set_password_callback([](std::size_t max_length, boost::asio::ssl::context::password_purpose purpose)
    {
        return "password";
    });

    ctx.use_certificate_file("cert.pem", boost::asio::ssl::context::pem);
    ctx.use_private_key_file("key.pem", boost::asio::ssl::context::pem);
    ctx.use_certificate_chain_file("chain.pem");
});

auto listener = std::unique_ptr<http_listener>(new http_listener(url, conf));