栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何访问要定义的Java grpc服务的请求元数据?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何访问要定义的Java grpc服务的请求元数据?

使用

ServerInterceptor
,然后通过传播身份
Context
。这使您可以制定中央身份验证策略。

拦截器可以从中检索身份

metadata headers
然后,应该验证
身份。然后可以
testHello
通过
io.grpc.Context
以下方式将经过验证的身份传达给应用程序(即):

class MyAuthInterceptor implements ServerInterceptor {  public static final Context.Key<Object> USER_IDENTITY      = Context.key("identity"); // "identity" is just for debugging  @Override  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(      ServerCall<ReqT, RespT> call,      metadata headers,      ServerCallHandler<ReqT, RespT> next) {    // You need to implement validateIdentity    Object identity = validateIdentity(headers);    if (identity == null) { // this is optional, depending on your needs      // Assume user not authenticated      call.close(Status.UNAUTENTICATED.withDescription("some more info"),      new metadata());      return new ServerCall.Listener() {};    }    Context context = Context.current().withValue(USER_IDENTITY, identity);    return Contexts.interceptCall(context, call, headers, next);  }}public class TestImpl extends TestServiceGrpc.TestServiceImplbase {  @Override  public void testHello(TestRequest req, StreamObserver<TestResponse> responseObserver) {    // Access to identity.    Object identity = MyAuthInterceptor.USER_IDENTITY.get();    ...  }}// Need to use ServerInterceptors to enable the interceptorServer server = ServerBuilder.forPort(PORT)    .addService(ServerInterceptors.intercept(new TestImpl(),        new MyAuthInterceptor()))    .build()    .start();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号