GoFrame框架使用jwt認證


gf-jwt插件地址:https://github.com/gogf/gf-jwt

GoFrame框架地址:https://goframe.org/display/gf

首先引入gf-jwt

下載安裝
$ go get github.com/gogf/gf-jwt

導入
import "github.com/gogf/gf-jwt"

定義自己的登錄鑒權以及jwt相關配置需要重寫gf-jwt的初始化函數

var GfJWTMiddleware *jwt.GfJWTMiddleware

func init() {
	middleware, err := jwt.New(&jwt.GfJWTMiddleware{
		Realm:         "KanXun_vr",                                        //領域名稱
		Key:           g.Cfg().GetBytes("JWT.MySecret"),                   //簽名秘鑰
		Timeout:       time.Minute * 60,                                   //過期時間
		MaxRefresh:    time.Minute * 5,                                    //token過期后,可憑借舊token獲取新token的刷新時間
		IdentityKey:   "id",                                               // 身份驗證的key值
		TokenLookup:   "header: Authorization, query: token, cookie: jwt", // token檢索模式,用於提取token-> Authorization
		TokenHeadName: "Bearer",                                           // token在請求頭時的名稱,默認值為Bearer
		// 客戶端在header中傳入Authorization 對一個值是Bearer + 空格 + token
		//TimeFunc:      time.Now,       // 測試或服務器在其他時區可設置該屬性
		Authenticator:   api.User.Login,          // 根據登錄信息對用戶進行身份驗證的回調函數
		Unauthorized:    auth.Unauthorized,       // 處理不進行授權的邏輯
		IdentityHandler: auth.IdentityHandler,    // 解析並設置用戶身份信息
		PayloadFunc:     auth.PayloadFunc,        // 登錄期間的回調的函數
		LoginResponse:   api.User.LoginResponse,  // 登錄成功后的響應,在此處添加數據到redis
		LogoutResponse:  api.User.LogoutResponse, //注銷/token無效化(黑名單)處理后返回的信息,用戶可自定義返回數據
	})
	if err != nil {
		g.Log().Error(err)
		return
	}
	GfJWTMiddleware = middleware
}

在此貼上gf-jwt相關的配置說明

https://goframe.org/pages/viewpage.action?pageId=6357048

重寫gf-jwt配置的登錄驗證方法

//Authenticator:   api.User.Login,           根據登錄信息對用戶進行身份驗證的回調函數

// Login 用戶登錄
func (*userApi) Login(r *ghttp.Request) (interface{}, error) {
	userName := r.GetString("username")
	id, err := service.Login(userName)
	if err != nil {
		//g.Log().Error("異常錯誤")
		if errors.Is(err, response.ErrorUserNotExist) {
			//response.JsonExit(r, 1, "no", "用戶不存在")
			return nil, response.ErrorUserNotExist
		}
		if errors.Is(err, response.ErrorDataException) {
			//response.JsonExit(r, 1, "no", "數據異常")
			return nil, response.ErrorDataException
		}
	}
	//到redis中查詢id,是否當前用戶登陸過
	userIS, err := dao.UsUser.QueryUserIDRedis(id)
	if userIS {
		//返回重復登錄,請重新登錄
		return nil, response.ErrorUserRepetition
	}
	//傳遞id值到r中
	r.SetParam("id", id)
	return g.Map{
		"id":       id,
		"username": userName,
	}, nil
}

重寫登錄成功后的響應

//LoginResponse:   api.User.LoginResponse,  登錄成功后的響應,在此處添加數據到redis


// LoginResponse 登錄成功后的響應函數,在此存儲數據到redis
func (*userApi) LoginResponse(r *ghttp.Request, code int, token string, expire time.Time) {
	//調用redis存儲數據
	err := dao.UsUser.SetUserNameRedis(token, gconv.Int64(r.GetParam("id")))
	if err != nil {
		g.Log().Error(" redis.Rdb.Set()異常", err)
		return
	}
	err = r.Response.WriteJson(g.Map{
		"code":   http.StatusOK,
		"token":  token,
		"expire": expire.Format(time.RFC3339),
	})
	if err != nil {
		g.Log().Error("r.Response.WriteJson()解析失敗", err)
		return
	}
	r.ExitAll()
}

重寫注銷token無效化處理,此處需配置MaxRefresh

// LogoutResponse:  api.User.LogoutResponse, 注銷/token無效化(黑名單)處理后返回的信息,用戶可自定義返回數據


// LogoutResponse 注銷成功,刪除redis中的數據
func (*userApi) LogoutResponse(r *ghttp.Request, code int) {
	err := dao.UsUser.DeleteUserIDRedis(gconv.Int64(r.GetParam("id")))
	if err != nil {
		g.Log().Error(" dao.UsUser.DeleteUserIDRedis()異常", err)
		return
	}
	err = r.Response.WriteJson(g.Map{
		"code":    code,
		"message": "success",
	})
	if err != nil {
		g.Log().Error("r.Response.WriteJson()解析失敗", err)
		return
	}
	r.ExitAll()
}

源代碼:https://gitee.com/zly233/gf-jwt.git


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM