关于golang:微服务从代码到k8s部署应有尽有系列三鉴权

5次阅读

共计 3339 个字符,预计需要花费 9 分钟才能阅读完成。

咱们用一个系列来解说从需要到上线、从代码到 k8s 部署、从日志到监控等各个方面的微服务残缺实际。

整个我的项目应用了 go-zero 开发的微服务,根本蕴含了 go-zero 以及相干 go-zero 作者开发的一些中间件,所用到的技术栈根本是 go-zero 项目组的自研组件,根本是 go-zero 全家桶了。

实战我的项目地址:https://github.com/Mikaelemmm…

1、鉴权服务

1.1 identity-api

identity 次要是用来做鉴权服务的,后面咱们 nginx 网关的时候有提到。在拜访一个资源的时候,nginx 外部会先来 identity-api 中解析 token,identity-api 会去申请 identity-rpc,所有的验证与颁发 token,对立是在 identity-rpc 中做的

咱们会从 header 的 Authorization 中获取 token,从 x -Original-Uri 获取拜访的资源门路

  • 如果以后拜访的路由须要登陆:

    • token 解析失败:就会返回给前端 http401 错误码;
    • token 解析胜利:就会将解析进去的 userId 放入 header 的 x -user 中返回给 auth 模块,auth 模块会把 header 传递给对应拜访的服务(usercenter), 这样咱们在 usercenter 间接就能够拿到该登陆用户的 id 了
  • 如果以后拜访的路由不须要登陆:

    • 前端 header 中传递了 token

      • 如果 token 校验失败:返回 http401;
      • 如果 token 校验胜利:就会将解析进去的 userId 放入 header 的 x -user 中返回给 auth 模块,auth 模块会把 header 传递给对应拜访的服务(usercenter), 这样咱们在 usercenter 间接就能够拿到该登陆用户的 id 了
    • 前端 header 中没传递 token:userid 会传递 0 给后端服务

urlNoAuth 办法判断以后资源是否在 yml 中配置能够不登陆

// 以后 url 是否须要受权验证
func (l *TokenLogic) urlNoAuth(path string) bool {
   for _, val := range l.svcCtx.Config.NoAuthUrls {
      if val == path {return true}
   }
   return false
}

isPass 办法就是去 identity-rpc 校验 token,次要也是应用了 go-zero 的 jwt 的办法

1.2 identity-rpc

当咱们在注册、登陆胜利时候,用户服务会调用 identity-rpc 生成 token,所以咱们对立在 identity-rpc 中颁发、校验 token,这样就不必每个服务都要写个 jwt 去保护。

当 identity-api 申请进来时候,identity-api 本人能够解析进去 userid,然而咱们要测验这个 token 是否是过期,就要去后端 rpc 中的 redis 中去进行二次校验(当然如果你感觉这里多一次申请,你能够把这一步放到 api 里间接申请 redis 也能够),通过 rpc 的 validateToken 办法校验

message ValidateTokenReq {
  int64 userId = 1;
  string token = 2;
}
message ValidateTokenResp {bool ok = 1;}

rpc validateToken(ValidateTokenReq) returns(ValidateTokenResp);

校验之前登陆、注册等受权时候颁发进来存在 redis 的 token 是否正确、过期。

这样 api 就能够返回给 nginx 的 auth 模块是否失败,如果失败 auth 会间接返回给前端 http code 401(所以你们前端应该是先判断 http 状态码 >=400 全副异样,再判断业务错误码), 如果胜利间接拜访后端服务了拿到数据间接返回给前端展现

2、装置 goctl 与 protoc、protoc-gen-go

【注】这个跟鉴权没什么关系,只是前面写代码要用到,在这里最好给装置了

2.1 装置 goctl

# for Go 1.15 and earlier
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl@latest

# for Go 1.16 and later
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest

验证是否装置胜利

$ goctl --version

Goctl 自定义模版 template:将我的项目目录下的 data/goctl 文件夹内容 copy 到 HOME 目录的 .goctl 中,goctl 在生成代码时候会优先依据这个模版下内容生成

$ cp -r data/goctl ~/.goctl

2.2 装置 protoc

链接:https://github.com/protocolbu…

间接找到对应平台的 protoc,我是 mac intel 芯片,所以间接找到 protoc-3.19.3-osx-x86_64.zip,解压进去后进入该目录下的 bin 目录中,将 protoc 间接 copy 到你的 gopath/bin 目录下即可。

验证是否装置胜利

$ protoc --version

2.3 装置 protoc-gen-go

$ GOPROXY=https://goproxy.cn/,direct go install google.golang.org/protobuf/cmd/protoc-gen-go@latest 

查看 $GOPATH/bin 下是否有 protoc-gen-go 即可

【注】:如果后续在应用 goctl 生成代码时候,遇到以下问题

protoc  --proto_path=/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc/pb usercenter.proto --go_out=plugins=grpc:/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc --go_opt=Musercenter.proto=././pb
goctl: generation error: unsupported plugin protoc-gen-go which installed from the following source:
google.golang.org/protobuf/cmd/protoc-gen-go, 
github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;

Please replace it by the following command, we recommend to use version before v1.3.5:
go get -u github.com/golang/protobuf/protoc-gen-go
goctl version: 1.3.0 darwin/amd64

间接执行

$ GOPROXY=https://goproxy.cn/,direct go get -u github.com/golang/protobuf/protoc-gen-go

2.4 装置 protoc-gen-go-grpc

$ GOPROXY=https://goproxy.cn/,direct go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

3、总结

总的来说,identity 还算是比较简单的,整个流程如下:

用户发动申请资源 -> nginx 网关 -> 匹配到对应服务模块 -> auth 模块 ->identity-api ->identity-rpc -> 用户申请的资源

我的项目地址

https://github.com/zeromicro/go-zero

欢送应用 go-zerostar 反对咱们!

微信交换群

关注『微服务实际 』公众号并点击 交换群 获取社区群二维码。

正文完
 0