1 Star 0 Fork 0

fkil555 / gin-extend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
checktoken.go 690 Bytes
一键复制 编辑 原始数据 按行查看 历史
fkil555 提交于 2024-04-28 11:02 . jwt token
package middlewares
import (
"net/http"
"gitee.com/fkil555/gin-extend/app/result"
"gitee.com/fkil555/gin-extend/helpers/tokenhelper"
"github.com/gin-gonic/gin"
)
func CheckToken() gin.HandlerFunc {
return func(ctx *gin.Context) {
header := ctx.Request.Header
token := header.Get("Authorization")
if len(token) <= 0 {
ctx.Abort()
ctx.JSON(http.StatusOK, result.Error(101, "need login token"))
return
}
jwtToken, err := tokenhelper.ParseJwtToken(token)
if err != nil || jwtToken == nil || jwtToken.Uid <= 0 {
ctx.Abort()
ctx.JSON(http.StatusOK, result.Error(102, "token parse fail"))
return
}
ctx.Set("loginUserId", jwtToken.Uid)
ctx.Next()
}
}
Go
1
https://gitee.com/fkil555/gin-extend.git
git@gitee.com:fkil555/gin-extend.git
fkil555
gin-extend
gin-extend
v0.0.15

搜索帮助