Class GrpcJwtInterceptor

java.lang.Object
io.github.xezzon.zeroweb.common.grpc.GrpcJwtInterceptor
All Implemented Interfaces:
io.grpc.ClientInterceptor, io.grpc.ServerInterceptor

@GlobalServerInterceptor @GlobalClientInterceptor @Component public class GrpcJwtInterceptor extends Object implements io.grpc.ServerInterceptor, io.grpc.ClientInterceptor

GrpcJwtInterceptor 用于在 gRPC 服务间调用中传递 JWT 认证信息。 它作为一个全局服务端拦截器和全局客户端拦截器,确保 JWT Claim 在调用链中正确地传递。

作为服务端拦截器,它从传入的 Metadata 中解析 JWT Claim,并将其绑定到当前的 gRPC Context 中。 这使得下游服务能够访问认证信息。

作为客户端拦截器,它从当前的 Context 中获取 JWT Claim,并将其添加到出站请求的 Metadata 中。 这确保了对其他服务的调用能够携带认证信息。

Author:
xezzon
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T,R> io.grpc.ClientCall<T,R>
    interceptCall(io.grpc.MethodDescriptor<T,R> method, io.grpc.CallOptions callOptions, io.grpc.Channel next)
    客户端拦截器,用于在 gRPC 客户端发起请求时添加认证信息。
    <T,R> io.grpc.ServerCall.Listener<T>
    interceptCall(io.grpc.ServerCall<T,R> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<T,R> next)
    服务端拦截器,用于在 gRPC 服务接收请求时处理认证信息。

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GrpcJwtInterceptor

      public GrpcJwtInterceptor()
  • Method Details

    • interceptCall

      public <T,R> io.grpc.ServerCall.Listener<T> interceptCall(io.grpc.ServerCall<T,R> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<T,R> next)
      服务端拦截器,用于在 gRPC 服务接收请求时处理认证信息。 它从请求头中提取 JWT Claim,并将其设置到当前的 Context 中, 以便在后续的处理中能够访问认证信息。
      Specified by:
      interceptCall in interface io.grpc.ServerInterceptor
      Type Parameters:
      T - 请求消息的类型。
      R - 响应消息的类型。
      Parameters:
      call - gRPC ServerCall 对象,代表当前的服务器调用。
      headers - 传入的 Metadata 头部信息,包含认证令牌。
      next - ServerCallHandler,用于继续处理请求。
      Returns:
      ServerCall.Listener,用于监听请求事件。
    • interceptCall

      public <T,R> io.grpc.ClientCall<T,R> interceptCall(io.grpc.MethodDescriptor<T,R> method, io.grpc.CallOptions callOptions, io.grpc.Channel next)
      客户端拦截器,用于在 gRPC 客户端发起请求时添加认证信息。 它从当前的 Context 中获取 JWT Claim,并将其添加到出站请求的 Metadata 中, 确保请求携带认证信息。
      Specified by:
      interceptCall in interface io.grpc.ClientInterceptor
      Type Parameters:
      T - 请求消息的类型。
      R - 响应消息的类型。
      Parameters:
      method - MethodDescriptor,代表调用的方法。
      callOptions - CallOptions,调用选项。
      next - Channel,用于发起新的调用。
      Returns:
      ClientCall,用于发起客户端调用。