gRPC:如何在客户端配置SSL?

时间:2017-12-04 07:17:11

标签: java ssl netty grpc grpc-java

它的新话题对我而言。我能够以纯文本连接。

public ManagedChannel getChannel(String serviceName){
    TSServiceClientManager scm = TSServiceManagementFactory.getInstance().getServiceClientManager();
    TSServiceConnectionInfo connInfo = scm.getServiceConnectionInfo(serviceName);
    if(channel == null){
        channel = ManagedChannelBuilder.forAddress(connInfo.getHost(), connInfo.getPort())
                .usePlaintext(true) //need help here for SSL code 
                .build();
    }

    return channel;
}

我被告知要启用客户端SSL。我知道如何生成,密钥库,信任库,pem,CA等。 我需要帮助:

如何启用SSL而不是.usePlaintext(true),如上面的代码所示?

(考虑到cert文件,密钥库,信任库和.pem文件存在,请重写代码)

我想知道服务器与SSL连接有什么关系吗?

1 个答案:

答案 0 :(得分:4)

您需要使用特定于传输的API。您今天可能正在使用Netty。对于Netty,您需要配置Netty的SslContext并将其传递给gRPC。您的用法可能如下所示:

SslContext sslcontext = GrpcSslContexts.forClient()
    // if server's cert doesn't chain to a standard root
    .trustManager(caFile)
    .keyManager(clientCertFile, keyFile) // client cert
    .build();
channel = NettyChannelBuilder.forAddress(serverHost, serverPort)
    .sslContext(sslContext)
    .build();

如果您需要服务器端配置,它将使用类似的NettyServerBuilder.sslContext()。但背景本身会有所不同。