代码拉取完成,页面将自动刷新
package rpc
import (
"context"
"google.golang.org/grpc"
)
type AuthFunc func(ctx context.Context, req interface{}, fullMethodName string) (context.Context, error)
// ServiceAuthOverride allows a given gRPC service implementation to override the global `AuthFunc`.
//
// If a service implements the AuthFuncOverride method, it takes precedence over the `AuthFunc` method,
// and will be called instead of AuthFunc for all method invocations within that service.
type ServiceAuthOverride interface {
AuthFuncOverride(ctx context.Context, req interface{}, fullMethodName string) (context.Context, error)
}
// UnaryServerInterceptor returns a new unary server interceptors that performs per-request auth.
func UnaryServerInterceptor(authFunc AuthFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
var newCtx context.Context
var err error
if overrideSrv, ok := info.Server.(ServiceAuthOverride); ok {
newCtx, err = overrideSrv.AuthFuncOverride(ctx, req, info.FullMethod)
} else {
newCtx, err = authFunc(ctx, req, info.FullMethod)
}
if err != nil {
return nil, err
}
return handler(newCtx, req)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。