java - 如何访问我正在定义的 java grpc 服务的请求元数据?

标签 java python request metadata grpc

对于某些背景,我正在尝试使用 grpc auth以便为我正在定义的某些服务提供安全性。

让我们看看我是否可以问这是一种有意义的方式。对于我的 python 代码,实现服务器端代码非常容易。

class TestServiceServer(service_pb2.TestServiceServer):

    def TestHello(self, request, context):

        ## credential metadata for the incoming request
        metadata = context.invocation_metadata()

        ## authenticate the user using the metadata

因此,如您所知,我能够很容易地从“上下文”中获取元数据。对我来说更难的是用 java 做同样的事情。

public class TestImpl extends TestServiceGrpc.TestServiceImplBase {

    @Override
    public void testHello(TestRequest req, StreamObserver<TestResponse> responseObserver) {

        // How do I get access to similar request metadata here?

        // from the parameter positions, it looks like it should be
        // "responseObserver" but that doesn't seem similar to "context"

    }

}

我承认我的问题来自几个方面。

1)我对Java不是很精通

2) 我大量使用 python 的“pdb”来调试类并查看我可以使用哪些方法。我不知道/不精通类似的 Java 工具。

3) 在这一点上,文档似乎相当稀疏​​。它向您展示了如何在服务器端设置 ssl 连接,但我找不到服务器查看请求元数据的示例,如我在 python 中所示。

有人可以告诉我如何执行此操作,或者可以向我展示一个与 python 的 pdb 相同的有用的 java 调试工具吗?

编辑/回答:

我需要先编写一个实现接口(interface) ServerInterceptor 的定义。

private class TestInterceptor implements ServerInterceptor {
    ....

然后,在实际绑定(bind)我的服务和构建我的服务器之前,我需要这样做。

TestImpl service = new TestImpl();
ServerServiceDefinition intercepted = ServerInterceptors.intercept(service, new TestInterceptor());

现在我可以创建服务器了。

server = NettyServerBuilder.forPort(port)

    // enable tls
    .useTransportSecurity(
        new File(serverCert),
        new File(serverKey)
    )
    .addService(
        intercepted  // had been "new TestImpl()"
    )
    .build();

server.start();

这允许我的 ServerInterceptor 在我触发客户端请求时实际被调用。

This link对解决这个问题很有帮助。

最佳答案

使用ServerInterceptor,然后通过Context 传播身份。这允许您拥有用于身份验证的中央策略。

拦截器可以从元数据 header 中检索身份。然后它应该验证身份。然后可以通过 io.grpc.Context 将经过验证的身份传递给应用程序(即 testHello):

/** Interceptor that validates user's identity. */
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 interceptor
Server server = ServerBuilder.forPort(PORT)
    .addService(ServerInterceptors.intercept(new TestImpl(),
        new MyAuthInterceptor()))
    .build()
    .start();

关于java - 如何访问我正在定义的 java grpc 服务的请求元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40112374/

相关文章:

java - 如何使用Java BO SDK 读取联合和合并的Webi 报告?

python - 使用 Pandas 读取带有时间戳列的 csv

python - Tkinter 中的小部件分层

python - 如何使用 python 登录需要服务器在第一次请求时响应 session ID 的页面?

java - Grails-在 Controller 中实现自动完成请求的反跳算法

javascript - Mootools 请求对象不允许我的查询字符串中有 '+' 标志

java - Android 是否执行 boolean 短路?

java - 调用 driver.findElement 还是调用 WebElement.findElement 更快?

java - JAX-RS Multipart 与 com.sun.jersey

python - 遍历字典列表并将项追加到数据框中的列的有效方法