feat: 初始化配置中心项目
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"kenaito-conf/server/internal/model"
|
||||
"kenaito-conf/server/internal/service"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func JWTAuth(authService *service.AuthService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "未登录"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
userInfo, err := authService.ValidateToken(tokenString)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "登录已过期"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("userInfo", userInfo)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserInfo(c *gin.Context) *model.LoginUserInfo {
|
||||
val, exists := c.Get("userInfo")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
return val.(*model.LoginUserInfo)
|
||||
}
|
||||
Reference in New Issue
Block a user