我们有一个 gPRC 服务,需要在类的 ThreadLocal 变量中设置身份验证/身份信息,才能正确调用另一个服务。gPRC 服务从请求中获取身份验证/身份信息,因此我正在考虑使用拦截器。
首先,我有一些代码如下所示。
public class ImpersonationInterceptor {
public <ReqT, RespT> interceptCall(
ServerCall<ReqT, RespT> serverCall, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
Principal principal = ... // get the identify from the request
AuthContext.setPrincipal(principal); // underneath it uses a ThreadLocal.
return next.startCall(
new SimpleForwardingServerCall<>(call) {
public void close(Status status, Metadata trailers) {
AuthContext.setPrincipal(null); // clear the identity
}
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
问题。