首页 > 解决方案 > 如何在 grpc-web 中使用 TLS?

问题描述

我目前正在尝试将 grpc-web 添加到 React-App 但我无法使其与 TLS 一起使用。我什至不知道如何正确导入客户端证书。

我正在使用带有 tls 的 grpc,并且通常设置正在运行。这意味着如果我使用一个简单的节点应用程序(无前端),我实现的所有服务都会按预期工作。

所以我知道使用@grpc/grpc-js 我可以做到以下几点:

import { credentials as _credentials } from '@grpc/grpc-js';
import { readFileSync } from 'fs';


const credentials = _credentials.createSsl(
    readFileSync(__dirname + '../certs/ca.crt'),
    readFileSync(__dirname + '../certs/client.key'),
    readFileSync(__dirname + '../certs/client.crt')
)

但是 grpc-web 没有凭证对象。我目前拥有的只是:

const { ProjectMeta, Affirm } = require('./base_pb.js');
const { ProjectClient } = require('./base_grpc_web_pb.js');

// No idea how to import these correctly
// const credentials = ???

let client = new ProjectClient('https://localhost:8443', credentials, null);

当我查看 base_grpc_web_pb.js 中 ProjectClient 的定义时,我更加困惑。

proto.base.ProjectClient =
    function(hostname, credentials, options) {
  if (!options) options = {};
  options.format = 'text';

  /**
   * @private @const {!grpc.web.GrpcWebClientBase} The client
   */
  this.client_ = new grpc.web.GrpcWebClientBase(options);

  /**
   * @private @const {string} The hostname
   */
  this.hostname_ = hostname;

};

甚至没有使用凭据参数???

在 grpcwebproxy 方面,我得到了:

2021/10/22 10:36:16 http: TLS handshake error from [::1]:59530: remote error: tls: unknown certificate

...我猜这是预期的?

希望任何人都可以帮助我解决这个问题。谢谢!

标签: reactjssslgrpcgrpc-nodegrpc-web

解决方案


推荐阅读