简体   繁体   English

CPP 中的 gRPC 提供 TLS 支持

[英]gRPC in CPP providing TLS support

Any examples of gRPC server using TLS in CPP??在 CPP 中使用 TLS 的 gRPC 服务器的任何示例?

I am trying to build a gRPC application.我正在尝试构建一个 gRPC 应用程序。 The server should provide TLS support if client wants to connect over TLS instead of TCP.如果客户端想要通过 TLS 而不是 TCP 连接,服务器应该提供 TLS 支持。

This is my server这是我的服务器

void RunServer() {
    std::string server_address("0.0.0.0:50051");
    GreeterServiceImpl service;
    ServerBuilder builder;
    std::shared_ptr<ServerCredentials> creds;

    if(enable_ssl)
    {
            grpc::SslServerCredentialsOptions::PemKeyCertPair pkcp ={"a","b"};
            grpc::SslServerCredentialsOptions ssl_opts;
            ssl_opts.pem_root_certs="";
            ssl_opts.pem_key_cert_pairs.push_back(pkcp);
            creds = grpc::SslServerCredentials(ssl_opts);
    }
    else
            creds=grpc::InsecureServerCredentials();
    // Listen on the given address without any authentication mechanism.
    builder.AddListeningPort(server_address, creds);
    // Register "service" as the instance through which we'll communicate with
    // clients. In this case it corresponds to an *synchronous* service.
    builder.RegisterService(&service);
    // Finally assemble the server.
    std::unique_ptr<Server> server(builder.BuildAndStart());

Error: undefined reference to grpc::SslServerCredetials(grpc::ssl_opts) I have included all the necessary files..错误:对 grpc::SslServerCredetials(grpc::ssl_opts) 的未定义引用我已经包含了所有必要的文件..

You code looks right.你的代码看起来不错。 If you are adapting from examples/cpp/helloworld , you need to change -lgrpc++_unsecure to -lgrpc++ in the Makefile.如果您是从examples/cpp/helloworld改编,则需要将 Makefile 中的-lgrpc++_unsecure -lgrpc++更改为-lgrpc++

For the benefits of others, an example of using the tls/ssl code can be found at https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50为了他人的利益,可以在https://github.com/grpc/grpc/blob/master/test/cpp/interop/server_helper.cc#L50找到使用 tls/ssl 代码的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM