✨ init project
This commit is contained in:
70
middleware/auth.go
Normal file
70
middleware/auth.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"Lee-WineList/core"
|
||||
"Lee-WineList/oauth2"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AuthorizeToken 验证OAuth2生成的Token
|
||||
func AuthorizeToken() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
// 判断有无token
|
||||
tokenStr := ctx.GetHeader("Authorization")
|
||||
if tokenStr == "" || !strings.HasPrefix(tokenStr, "Bearer ") {
|
||||
core.R(ctx).FailWithMessageAndCode("请先登录", http.StatusUnauthorized)
|
||||
ctx.Abort()
|
||||
return
|
||||
}
|
||||
// 先取出用户Token
|
||||
token, err := oauth2.OAuthServer.ValidationBearerToken(ctx.Request)
|
||||
if err != nil {
|
||||
log.Errorf("获取Token失败,错误:%s", err.Error())
|
||||
core.R(ctx).FailWithMessageAndCode("登录已失效或已在其他地方登录", http.StatusUnauthorized)
|
||||
ctx.Abort()
|
||||
return
|
||||
}
|
||||
// 把UserId字段反序列化成map
|
||||
//info := make(map[string]string)
|
||||
//if err = json.Unmarshal([]byte(token.GetUserID()), &info); err != nil {
|
||||
// core.R(ctx).FailWithMessageAndCode("Token数据解析失败", http.StatusUnauthorized)
|
||||
// ctx.Abort()
|
||||
// return
|
||||
//}
|
||||
//go func() {
|
||||
// // 异步记录用户在线情况,十分钟没操作就是不在线了
|
||||
// rdsKey := "oauth:online:" + info["userId"]
|
||||
// global.RedisConn.Set(context.Background(), rdsKey, "1", 10*time.Minute)
|
||||
//}()
|
||||
// 判断通过,允许放行
|
||||
ctx.Request.Header.Add("userId", token.GetUserID())
|
||||
ctx.Set("userId", token.GetUserID())
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// DealLoginUserId 处理登录用户Id
|
||||
func DealLoginUserId() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
// 判断有无token
|
||||
tokenStr := ctx.GetHeader("Authorization")
|
||||
if tokenStr == "" || !strings.HasPrefix(tokenStr, "Bearer ") {
|
||||
//ctx.Next()
|
||||
return
|
||||
}
|
||||
// 先取出用户Token
|
||||
token, err := oauth2.OAuthServer.ValidationBearerToken(ctx.Request)
|
||||
if err != nil {
|
||||
//ctx.Next()
|
||||
return
|
||||
}
|
||||
//log.Debugf("本次请求存在正常Token: %v", tokenStr)
|
||||
// 判断通过,允许放行
|
||||
ctx.Request.Header.Add("userId", token.GetUserID())
|
||||
ctx.Set("userId", token.GetUserID())
|
||||
//ctx.Next()
|
||||
}
|
||||
}
|
29
middleware/request.go
Normal file
29
middleware/request.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"Lee-WineList/core"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type request struct{}
|
||||
|
||||
// Request Open
|
||||
func Request() *request {
|
||||
return &request{}
|
||||
}
|
||||
|
||||
// NotInternalRequest 检查是否是内部调用
|
||||
func (request) NotInternalRequest() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
// 取出来源
|
||||
from := ctx.Request.Header.Get("X-Request-From")
|
||||
if from != "internal" {
|
||||
// 如果请求不是内部请求,直接返回请求不合法
|
||||
core.R(ctx).FailWithMessageAndCode("请求不合法", http.StatusBadRequest)
|
||||
ctx.Abort()
|
||||
return
|
||||
}
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user