1 Star 0 Fork 0

xiedongji/antgo

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_auth_mw.go 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
xiedongji 提交于 2023-03-18 22:09 +08:00 . 整理文件
package xmw
import (
"errors"
"gitee.com/xiedongji/antgo/helper/xstring"
"gitee.com/xiedongji/antgo/helper/xtoken"
"gitee.com/xiedongji/antgo/helper/xutils"
"github.com/gin-gonic/gin"
)
/* ============================================================ *
* 功能:API鉴权
* 类型:中间件
* 举例: "/land/login","/land/logout", "/ping","/res/"
* ============================================================ */
const (
AuthKey = "Authorization" // 请求头部关键字
SessionKey = "session_key" //
)
func ApiAuthMiddleware(allowUrls ...string) gin.HandlerFunc {
return func(ctx *gin.Context) {
// 默认允许的url
if len(allowUrls) > 0 {
if xstring.InArrayString(ctx.Request.URL.Path, allowUrls) ||
xstring.InArrayPath(ctx.Request.URL.Path, allowUrls) {
ctx.Next()
return
}
}
token := ctx.GetHeader(AuthKey)
if token == "" {
xutils.ResponseError(ctx, errors.New("登录标识丢失"))
ctx.Abort()
return
}
sessionInfo, err := xtoken.ParseToken(token)
if err != nil {
xutils.ResponseErrorCode(ctx, xutils.CodeUnLogin, err)
ctx.Abort()
return
}
if sessionInfo == nil {
xutils.ResponseError(ctx, errors.New("登录信息解析失败"))
ctx.Abort()
return
}
if sessionInfo.UserName == "" || sessionInfo.UserId == 0 {
xutils.ResponseError(ctx, errors.New("登录信息解析失败2"))
ctx.Abort()
return
}
// 设置登录信息
ctx.Set(SessionKey, sessionInfo)
ctx.Next()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiedongji/antgo.git
git@gitee.com:xiedongji/antgo.git
xiedongji
antgo
antgo
v1.0.18

搜索帮助