feat: RBAC 基本完成
This commit is contained in:
+20
-20
@@ -1,12 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system"
|
||||
systemReq "sundynix-go/model/system/request"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetLoginToken 获取登录token
|
||||
@@ -57,22 +57,22 @@ func GetToken(c *gin.Context) string {
|
||||
// 从请求头中获取Authorization字段的值
|
||||
token := c.Request.Header.Get("Authorization")
|
||||
|
||||
// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token
|
||||
if token == "" {
|
||||
j := NewJWT()
|
||||
token, _ = c.Cookie("x-token")
|
||||
|
||||
// 解析并验证token的有效性
|
||||
claims, err := j.ParseToken(token)
|
||||
if err != nil {
|
||||
// 如果解析失败,记录错误日志并返回当前token
|
||||
global.Logger.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||
return token
|
||||
}
|
||||
|
||||
// 如果token有效,则将其重新写入Cookie,并设置过期时间
|
||||
SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||
}
|
||||
//// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token
|
||||
//if token == "" {
|
||||
// j := NewJWT()
|
||||
// token, _ = c.Cookie("x-token")
|
||||
//
|
||||
// // 解析并验证token的有效性
|
||||
// claims, err := j.ParseToken(token)
|
||||
// if err != nil {
|
||||
// // 如果解析失败,记录错误日志并返回当前token
|
||||
// global.Logger.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||
// return token
|
||||
// }
|
||||
//
|
||||
// // 如果token有效,则将其重新写入Cookie,并设置过期时间
|
||||
// SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||
//}
|
||||
|
||||
// 返回获取到的token
|
||||
return token
|
||||
@@ -127,10 +127,10 @@ func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
||||
//
|
||||
// 返回值:
|
||||
// - uint: 返回用户的 ID,如果获取失败则返回 0。
|
||||
func GetUserId(c *gin.Context) uint {
|
||||
func GetUserId(c *gin.Context) string {
|
||||
if claims, exists := c.Get("claims"); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return 0
|
||||
return "0"
|
||||
} else {
|
||||
return cl.BaseClaims.ID
|
||||
}
|
||||
|
||||
+11
-5
@@ -1,14 +1,20 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sundynix-go/utils/uniqueid"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashPwd(t *testing.T) {
|
||||
//hash := BcryptHash("admin")
|
||||
//fmt.Println(hash) // $2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2
|
||||
//
|
||||
//check := BcryptCheck("admin", "$2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2")
|
||||
//fmt.Println(check)
|
||||
hash := BcryptHash("sundynix")
|
||||
fmt.Println(hash) // $2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2
|
||||
|
||||
check := BcryptCheck("admin", "$2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2")
|
||||
fmt.Println(check)
|
||||
}
|
||||
|
||||
func TestUuid(t *testing.T) {
|
||||
id := uniqueid.GenerateId()
|
||||
fmt.Println(id)
|
||||
}
|
||||
|
||||
+2
-1
@@ -2,10 +2,11 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system/request"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
type JWT struct {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package uniqueid
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func GenerateId() string {
|
||||
uuidV1, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return uuidV1.String()
|
||||
}
|
||||
Reference in New Issue
Block a user