我在c ++ grpc项目中创建了一个自定义的Inceptceptor,以检查Authorization Bearer令牌是否有效或用户已通过身份验证,因此,我创建了一个拦截器来获取元数据并验证令牌,如果令牌可以的话,我执行继续使用方法,此时一切正常,但是...
当令牌无效时,如何从拦截器返回grpc :: Status :: Unauthenticateded错误?
我尝试使用method.ModifySendMessage和状态,但无法正常工作并崩溃
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 << "\n";
//continue
methods->Proceed();
} else {
std::cout << " token is no valid " << token << "\n";
// 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");
}
}
}
}
我怎么能回来 返回Status(grpc :: StatusCode :: UNAUTHENTICATED,e.what()); 当令牌在拦截器中无效时