系列
- 云原生 API 网关,gRPC-Gateway V2 初探
- Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务:第一篇
- Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务:第二篇
- Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务(三):RSA(RS512) 签名 JWT
客户端强类型约束,自动生成 API TS 类型定义
protobufjs
官方文档:protobufjs
安装:
1 | sh复制代码yarn add protobufjs |
node_modules/.bin
会多出如下命令:
pbjs
pbts
根据 auth.proto
生成 API TS 类型定义
1 | sh复制代码PROTO_PATH=../microsvcs/auth/api |
脚本已被放置在 miniprogram/gen_ts.sh
,在 miniprogram
目录执行 sh gen_ts.sh
即可生成如下文件:
miniprogram/miniprogram/service/proto_gen/auth/auth_pb.js
miniprogram/miniprogram/service/proto_gen/auth/auth_pb.d.ts
修改 app.ts
引入:
1 | ts复制代码import { auth } from "./service/proto_gen/auth/auth_pb" |
在文件里面做如下改动:
从上图可以看到有属性提示。这里我们也加入了一个 camelcase-keys
包。它主要用来将属性 key
从网络上传输的 expires_in
转换为 expiresIn
。
Token 验证
编码实战
具体代码位于:microsvcs/shared/auth/token/token.go
1 | go复制代码type JWTTokenVerifier struct { |
测试用例
- 正常
token
过期- 坏的
token
- 签名错误
具体代码位于:microsvcs/shared/auth/token/token_test.go
Refs
- API Security : API key is dead..Long live Distributed Token by value
- Demo: go-grpc-gateway-v2-microservice
- gRPC-Gateway
- gRPC-Gateway Docs
1 | sh复制代码我是为少 |
本文转载自: 掘金