嗨,我需要在grpc c ++中做到这一点:
客户端在元数据中发送一个Authorization:Bearer令牌
在服务器中,我需要:
在解决请求之前拦截或处理该元数据
获取元数据以查找是否存在自动生成键/值,并让我自己的逻辑来验证令牌,如果令牌有效,则继续解决该请求,如果令牌无效,则返回返回Status(grpc :: StatusCode ::未经授权,消息);
我已经有自己的逻辑来验证令牌。
我看到这篇文章:Intercept gRPC C++ calls in server and client
我创建了一个自定义AuthMetadataProcessor的正确答案
但是我不知道如何使用grpc :: InsecureServerCredentials()来实现或设置自定义AuthMetadataProcessor并以此启动服务器。
我尝试:
void RunServer() {
auto cred = grpc::InsecureServerCredentials();
cred.get()->SetAuthMetadataProcessor(
std::shared_ptr<grpc::AuthMetadataProcessor>(new grpc::MyServiceAuthProcessor(true)));
//server address
std::string server_address("0.0.0.0:8080");
//our PermissionController service
PermissionController permissionController;
// create the server
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, cred);
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&permissionController);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
server->Wait();
}
但是服务器没有启动显示此错误:
E0712 17:02:20.429173000 1 insecure_server_credentials.cc:34]断言失败:0
我想知道它是如何实现它的正确方法。
谢谢。
答案 0 :(得分:0)
到目前为止,grpc::AuthMetadataProcessor
不能与InsecureServerCredentials
一起使用,这很不幸,因为在Envoy边车(在TLS后面)上运行gRPC服务时会很高兴可以终止。