【发布时间】:2019-07-12 17:03:42
【问题描述】:
大家好,我需要在 grpc c++ 中执行此操作:
客户端发送授权:元数据中的不记名令牌
在服务器中我需要:
在解析请求之前拦截或处理该元数据
获取元数据查找是否存在自动化键/值并制作我自己的逻辑来验证令牌,如果令牌有效,则继续解析请求,如果令牌无效,则返回 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
我想知道它的正确实现方式。
谢谢。
【问题讨论】: