共計 3194 個字符,預計需要花費 8 分鐘才能閱讀完成。
本篇文章給大家分享的是有關如何在 go-zero 中使用 jwt-token 鑒權實踐,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
創建項目生成 go.mod 文件
以如下指令創建項目
mkdir jwttoken
cd jwttoken
go mod init jwttoken
定義 user.api
本文設計 API 如下 | 描述 | 格式 | 方法 | 參數 | 返回 | 是否需要鑒權 | |—-|—-|—-|—-|—-|—-| | 用戶登錄 |/open/authorization|post|mobile: 手機號,passwd: 密碼,code: 圖片驗證碼 |id: 用戶 ID,token: 用戶 token| 否 | | 更新用戶信息 |/user/update|post|mobile: 用戶手機號 |token: 用戶新的 token| 是 |
根據以上描述, 書寫 api 的模板文件如下
type (
UserOptReq struct {
mobile string `form: mobile `
passwd string `form: passwd `
code string `form: code,optional `
UserOptResp struct {
id uint `json: id `
token string `json: token `
// 修改
UserUpdateReq struct {
id uint `form: id `
mobile string `form: mobile,optional `
service user-api {
@server(
handler: authorizationHandler
folder: open
post /open/authorization(UserOptReq) returns(UserOptResp)
@server(
handler: edituserHandler
folder: user
post /user/update(UserUpdateReq) returns(UserOptResp)
}
注意
一個文件里面只能有一個 service
工具最后會以 type 里面模型為樣板生成各種結構體, 所以參數和結構體保持一致即可
如果我們需要分文件夾管理業務, 可以用 folder 屬性來定義
生成代碼
采用如下指令生成代碼
goctl api go -api user.api -dir .
運行一下
go run open.go
測試一下
curl http://127.0.0.1:8888/open/authorization -X POST -d mobile=15367151352 passwd=123rte code=asasa \ passwd\ :\ testpwd\ ,\ code\ :\ asdf\ }
{id :0, token :}
中間件實現鑒權
在 handler 下新建 auth.go 文件, 關鍵代碼如下
// 鑒權白名單, 在這里面的是不需要鑒權的
var whiteList []string = []string{
/open/ ,
// 鑒權中間件
func Auth(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {w.Header().Add(X-Middleware , auth)
uri := r.RequestURI
// 默認不在
isInWhiteList := false
// 判斷請求是否包含白名單中的元素
for _, v := range whiteList {if strings.Contains(uri, v) {
isInWhiteList = true
// 如果愛白名單里面直接通過
if isInWhiteList {next(w, r)
return
// 否則獲取前端 header 里面的 X -Token 字段, 這個就是 token
token := r.Header.Get(X-Token)
// 工具類見 util\jwttoken.go
_, err := utils.DecodeJwtToken(token)
// 如果有錯直接返回 error
if err != nil {httpx.Error(w, err)
return
// 沒報錯就繼續
next(w, r)
}
在 routers.go 中添加一行代碼
func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
// 添加這行代碼
engine.Use(Auth)
///。。}
生成 jwttoken
在 logic\open\authorizationlogic.go 中實現 jwttoken 的獲取
func (l *AuthorizationLogic) Authorization(req types.UserOptReq) (*types.UserOptResp, error) {
// 這個是生成 jwttoken 的工具類
token, err := utils.EncodeJwtToken(map[string]interface{}{
role : kefu ,
id : 10086 ,
return types.UserOptResp{Token: token,}, err
}
測試不攜帶 token 時訪問
curl http://127.0.0.1:8888/user/update -X POST -d mobile=15367151352 id=123
鑒權失敗, 缺少鑒權參數
獲取 token
curl http://127.0.0.1:8081/open/authorization -X POST -d mobile=15367151352 passwd=123rte code=asasa
{id :1599063149, token : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTkzMjIzNDksImlkIjoiMTUzNjcxNTEzNTIifQ.jcdg3c2rdigPO5ZTxcDilVGERAuMIdY9BUmMNX3ZA9c}
攜帶 token 時訪問
curl http://127.0.0.1:8888/user/update -X POST -H X-Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTkzMjIzNDksImlkIjoiMTUzNjcxNTEzNTIifQ.jcdg3c2rdigPO5ZTxcDilVGERAuMIdY9BUmMNX3ZA9c -d mobile=15367151352 id=123
# 請求成功
{id :123, token :}
攜帶錯誤的 token 時訪問
curl http://127.0.0.1:8888/user/update -X POST -H X-Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTkzMjIzNDksImlkIjoiMTUzNjcxNTEzNTIifQ.jcdg3c2rdigPO5ZTxcDilVGERAuMIdY9BUmMNX3ZA9c0000 -d mobile=15367151352 id=123
# 返回簽名無效
signature is invalid
以上就是如何在 go-zero 中使用 jwt-token 鑒權實踐,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。