C++GRPC::实验性:拦截器如何从自定义拦截器返回状态和消息

C++ grpc::experimental:interceptor how to return status and message from custom interceptor(C++GRPC::实验性:拦截器如何从自定义拦截器返回状态和消息)
本文介绍了C++GRPC::实验性:拦截器如何从自定义拦截器返回状态和消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的c++GRPC项目中创建一个自定义的inteceptor,以检查授权持有者令牌是否有效或用户是否经过身份验证,因此我创建了一个拦截器来获取元数据并验证令牌,如果令牌是OK的,我将执行继续方法,此时一切正常,但是...

当令牌无效时,如何从侦听器返回GRPC::Status::未经身份验证的错误?

我尝试了方法。ModifySendMessage和Status,但它不工作和崩溃

void AuthInterceptor::Intercept(grpc::experimental::InterceptorBatchMethods *methods) {

    if (methods->QueryInterceptionHookPoint(
            grpc::experimental::InterceptionHookPoints::POST_RECV_INITIAL_METADATA)) {
        auto map = methods->GetRecvInitialMetadata();

        std::multimap<grpc::string_ref, grpc::string_ref>::iterator itr;

        for (itr = map->begin(); itr != map->end(); ++itr) {
            if (itr->first == "authorization") {

                std::string erase = "Bearer ";
                std::size_t pos = itr->second.find(erase);
                std::string value = itr->second.data();
                std::string token = value.erase(pos, erase.length());

                TokenStatus tokenStatus = Security::Token::validateToken(token);

                if (tokenStatus.valid) {
                    std::cout << " token is valid " << token << "
";
                    //continue 
                    methods->Proceed();
                } else {
                    std::cout << " token is no valid " << token << "
";
                 // i need to return or throw
              // return Status(grpc::StatusCode::UNAUTHENTICATED, "you are not UNAUTHENTICATED");


                }


            } else {
                 // i need to return or throw
              // return Status(grpc::StatusCode::UNAUTHENTICATED, "you are not UNAUTHENTICATED");
            }

        }

    }


}

我如何才能返回 返回状态(GRPC::StatusCode::未验证,e.What()); 拦截器中的令牌无效时

推荐答案

这是旧的,但请尝试methods->ModifySendStatus(grpc::Status const & status);

这篇关于C++GRPC::实验性:拦截器如何从自定义拦截器返回状态和消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

socks4 with asynchronous boost::asio(带有异步Boost::ASIO的socks4)
Boost ASIO HTTP client POST(增强ASIO HTTP客户端开机自检)
asio::strandlt;asio::io_context::executor_typegt; vs io_context::strand(Asio::strandlt;asio::io_context::executor_typegt;VS io_CONTEXT::Strand)
What#39;s the difference between asio::io_context and asio::thread_pool?(ASIO::IO_CONTEXT和ASIO::THREAD_POOL有什么区别?)
How to use boost::asio with Linux GPIOs(如何在Linux GPIO中使用Boost::ASIO)
Boost asio io_service vs io_context(增强ASIO io_服务与io_Context)