This commit is contained in:
zhaoxiaorong 2025-04-09 10:19:15 +08:00
parent c7f24e3b6d
commit 51ff7d1ffd
3 changed files with 51 additions and 23 deletions

43
middleware/jwt.go Normal file
View File

@ -0,0 +1,43 @@
package middleware
import (
"log"
"net/http"
"git.apinb.com/bsm-sdk/core/crypto/encipher"
"github.com/gin-gonic/gin"
)
func JwtAuth(redisToken string) gin.HandlerFunc {
return func(c *gin.Context) {
// 从请求头中获取 Authorization
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
log.Println("获取token异常:", "Authorization header is required")
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
c.Abort()
return
}
// 提取Token
claims, err := encipher.ParseTokenAes(authHeader)
if err != nil || claims == nil {
log.Println("提取token异常:", "Token is required")
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is required"})
c.Abort()
return
}
// 从redis 获取token,判断当前redis 是否为空
if redisToken == "" {
log.Println("redis异常", "Token status unauthorized")
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token status unauthorized"})
c.Abort()
return
}
// 将解析后的 Token 存储到上下文中
c.Set("Auth", claims)
// 如果 Token 有效,继续处理请求
c.Next()
}
}

View File

@ -5,25 +5,18 @@ import (
"git.apinb.com/bsm-sdk/core/crypto/encipher" "git.apinb.com/bsm-sdk/core/crypto/encipher"
"git.apinb.com/bsm-sdk/core/errcode" "git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/types"
"git.apinb.com/bsm-sdk/core/utils" "git.apinb.com/bsm-sdk/core/utils"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
type Meta struct {
ID uint `json:"id"`
IDENTITY string `json:"identity"`
EXTEND map[string]string `json:"extend"`
CLIENT string `json:"client"`
ROLE string `json:"role"`
}
// 解析Context中MetaData的数据 // 解析Context中MetaData的数据
type ParseOptions struct { type ParseOptions struct {
RoleValue string // 判断角色的值 RoleValue string // 判断角色的值
MustPrivateAllow bool // 是否只允许私有IP访问 MustPrivateAllow bool // 是否只允许私有IP访问
} }
func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*Meta, error) { func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*types.JwtClaims, error) {
// 解析metada中的信息并验证 // 解析metada中的信息并验证
md, ok := metadata.FromIncomingContext(ctx) md, ok := metadata.FromIncomingContext(ctx)
if !ok { if !ok {
@ -40,34 +33,26 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*Meta, error) {
return nil, err return nil, err
} }
meta := &Meta{
ID: claims.ID,
IDENTITY: claims.Identity,
CLIENT: claims.Client,
EXTEND: claims.Extend,
ROLE: claims.Role,
}
if opts != nil { if opts != nil {
if !meta.CheckRole("role", opts.RoleValue) { if !checkRole(claims, "role", opts.RoleValue) {
return nil, errcode.ErrPermissionDenied return nil, errcode.ErrPermissionDenied
} }
if opts.MustPrivateAllow { if opts.MustPrivateAllow {
if utils.IsPublicIP(meta.CLIENT) { if utils.IsPublicIP(claims.Client) {
return nil, errcode.ErrPermissionDenied return nil, errcode.ErrPermissionDenied
} }
} }
} }
return meta, nil return claims, nil
} }
func (m *Meta) CheckRole(roleKey, roleValue string) bool { func checkRole(claims *types.JwtClaims, roleKey, roleValue string) bool {
if roleValue == "" { if roleValue == "" {
return true return true
} }
if role, exists := m.EXTEND[roleKey]; !exists || role != roleValue { if role, exists := claims.Extend[roleKey]; !exists || role != roleValue {
return false return false
} else { } else {
return true return true

View File

@ -31,7 +31,7 @@ type (
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"` DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
Status int64 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常 Status int8 `gorm:"default:0;index;" json:"status"` // 状态默认为0-1禁止1为正常
} }
// standard ID,Identity,Created,Updated,Deleted,Status definition. // standard ID,Identity,Created,Updated,Deleted,Status definition.