Go+gRPC-Gateway(V2) 微服务实战,小程序登录鉴权服务(五):鉴权 gRPC-Interceptor 拦截器实战

拦截器(gRPC-Interceptor)类似于 Gin 中间件(Middleware),让你在真正调用 RPC 服务前,进行身份认证、参数校验、限流等通用操作。

系列

  1. 云原生 API 网关,gRPC-Gateway V2 初探
  2. Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务:第一篇
  3. Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务:第二篇
  4. Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务(三):RSA(RS512) 签名 JWT
  5. Go+gRPC-Gateway(V2) 微服务实战,小程序登录鉴权服务(四):自动生成 API TS 类型

grpc.UnaryInterceptor

VSCode -> Go to Definition 开始,我们看到如下源码:

// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the
// server. Only one unary interceptor can be installed. The construction of multiple
// interceptors (e.g., chaining) can be implemented at the caller.
func UnaryInterceptor(i UnaryServerInterceptor) ServerOption {
	return newFuncServerOption(func(o *serverOptions) {
		if o.unaryInt != nil {
			panic("The unary server interceptor was already set and may not be reset.")
		}
		o.unaryInt = i
	})
}

注释很清晰:UnaryInterceptor 返回一个为 gRPC server 设置 UnaryServerInterceptorServerOption。只能安装一个一元拦截器。多个拦截器的构造(例如,chaining)可以在调用方实现。

这里我们需要实现具有如下定义的方法:

// UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
// contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
// of the service method implementation. It is the responsibility of the interceptor to invoke handler
// to complete the RPC.
type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)

注释很清晰:UnaryServerInterceptor 提供了一个钩子来拦截服务器上一元 RPC 的执行。info 包含拦截器可以操作的这个 RPC 的所有信息。handlerservice 方法实现的包装器。拦截器的职责是调用 handler 来完成 RPC 方法的执行。在真正调用 RPC 服务前,进行各微服务的通用操作(如:authorization)。

Auth Interceptor 编写

一句话描述业务:

  • 从请求头(header) 中拿到 authorization 字段传过来的 token,然后通过 pubclic.key 验证是否合法。合法就把 AccountID(claims.subject) 附加到当前请求上下文中(context)。

核心拦截器代码如下:

type interceptor struct {
	verifier tokenVerifier
}
func (i *interceptor) HandleReq(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    // 拿到 token
	tkn, err := tokenFromContext(ctx)
	if err != nil {
		return nil, status.Error(codes.Unauthenticated, "")
	}
    
    // 验证 token
	aid, err := i.verifier.Verify(tkn)
	if err != nil {
		return nil, status.Errorf(codes.Unauthenticated, "token not valid: %v", err)
	}
	// 调用真正的 RPC 方法
	return handler(ContextWithAccountID(ctx, AccountID(aid)), req)
}

具体代码位于 /microsvcs/shared/auth/auth.go

Todo 微服务

一个 Todo-List 测试服务。

这里,我们加入一个新的微服务 Todo,我们要做的是:访问 Todo RPC Service 之前需要经过我们的鉴权 Interceptor 判断是否合法。

定义 proto

todo.proto

syntax = "proto3";
package todo.v1;
option go_package="server/todo/api/gen/v1;todopb";
message CreateTodoRequest {
    string title = 1;
}
message CreateTodoResponse {
}
service TodoService {
    rpc CreateTodo (CreateTodoRequest) returns (CreateTodoResponse);
}

简单起见(测试用),这里就一个字段 title

定义 google.api.Service

todo.yaml

type: google.api.Service
config_version: 3

http:
  rules:
  - selector: todo.v1.TodoService.CreateTodo
    post: /v1/todo
    body: "*"

生成相关代码

microsvcs 目录下执行:

sh gen.sh

会生成如下文件:

  • microsvcs/todo/api/gen/v1/todo_grpc.pb.go
  • microsvcs/todo/api/gen/v1/todo.pb.go
  • microsvcs/todo/api/gen/v1/todo.pb.gw.go

client 目录下执行:

sh gen_ts.sh

会生成如下文件:

  • client/miniprogram/service/proto_gen/todo/todo_pb.js
  • client/miniprogram/service/proto_gen/todo/todo_pb.d.ts

实现 CreateTodo Service

具体见:microsvcs/todo/todo/todo.go

type Service struct {
	Logger *zap.Logger
	todopb.UnimplementedTodoServiceServer
}
func (s *Service) CreateTodo(c context.Context, req *todopb.CreateTodoRequest) (*todopb.CreateTodoResponse, error) {
    // 从 token 中解析出 accountId,确定身份后执行后续操作
	aid, err := auth.AcountIDFromContext(c)
	if err != nil {
		return nil, err
	}
	s.Logger.Info("create trip", zap.String("title", req.Title), zap.String("account_id", aid.String()))
	return nil, status.Error(codes.Unimplemented, "")
}

重构下 gRPC-Server 的启动

我们现在有多个服务了,Server 启动部分有很多重复的,重构一下:

具体代码位于:microsvcs/shared/server/grpc.go

func RunGRPCServer(c *GRPCConfig) error {
	nameField := zap.String("name", c.Name)
	lis, err := net.Listen("tcp", c.Addr)
	if err != nil {
		c.Logger.Fatal("cannot listen", nameField, zap.Error(err))
	}
	var opts []grpc.ServerOption
	// 鉴权微服务是无需 auth 拦截器,这里做一下判断
	if c.AuthPublicKeyFile != "" {
		in, err := auth.Interceptor(c.AuthPublicKeyFile)
		if err != nil {
			c.Logger.Fatal("cannot create auth interceptor", nameField, zap.Error(err))
		}
		opts = append(opts, grpc.UnaryInterceptor(in))
	}
	s := grpc.NewServer(opts...)
	c.RegisterFunc(s)
	c.Logger.Info("server started", nameField, zap.String("addr", c.Addr))
	return s.Serve(lis)
}

接下,其它微服务的gRPC-Server启动代码就好看很多了:

具体代码位于:todo/main.go

logger.Sugar().Fatal(
    server.RunGRPCServer(&server.GRPCConfig{
    	Name:              "todo",
    	Addr:              ":8082",
    	AuthPublicKeyFile: "shared/auth/public.key",
    	Logger:            logger,
    	RegisterFunc: func(s *grpc.Server) {
    		todopb.RegisterTodoServiceServer(s, &todo.Service{
    			Logger: logger,
    		})
    	},
    }),
)

具体代码位于:auth/main.go

logger.Sugar().Fatal(
	server.RunGRPCServer(&server.GRPCConfig{
		Name:   "auth",
		Addr:   ":8081",
		Logger: logger,
		RegisterFunc: func(s *grpc.Server) {
			authpb.RegisterAuthServiceServer(s, &auth.Service{
				OpenIDResolver: &wechat.Service{
					AppID:     "your-appid",
					AppSecret: "your-appsecret",
				},
				Mongo:          dao.NewMongo(mongoClient.Database("grpc-gateway-auth")),
				Logger:         logger,
				TokenExpire:    2 * time.Hour,
				TokenGenerator: token.NewJWTTokenGen("server/auth", privKey),
			})
		},
	}),
)

联调

重构下 gateway server

我们要反向代理到多个 gRPC server 端点了,整理下代码,弄成配置的形式:

具体代码位于:microsvcs/gateway/main.go

serverConfig := []struct {
	name         string
	addr         string
	registerFunc func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error)
}{
	{
		name:         "auth",
		addr:         "localhost:8081",
		registerFunc: authpb.RegisterAuthServiceHandlerFromEndpoint,
	},
	{
		name:         "todo",
		addr:         "localhost:8082",
		registerFunc: todopb.RegisterTodoServiceHandlerFromEndpoint,
	},
}

for _, s := range serverConfig {
	err := s.registerFunc(
		c, mux, s.addr,
		[]grpc.DialOption{grpc.WithInsecure()},
	)
	if err != nil {
		logger.Sugar().Fatalf("cannot register service %s : %v", s.name, err)
	}
}
addr := ":8080"
logger.Sugar().Infof("grpc gateway started at %s", addr)
logger.Sugar().Fatal(http.ListenAndServe(addr, mux))

测试

Refs

我是为少
微信:uuhells123
公众号:黑客下午茶
加我微信(互相学习交流),关注公众号(获取更多学习资料~)
posted @ 2021-04-19 09:57  为少  阅读(1466)  评论(0编辑  收藏  举报