🎨jwt的secret每个用户不同

This commit is contained in:
coward 2024-07-12 11:06:14 +08:00
parent d74d7c579f
commit 172cb46c31
2 changed files with 16 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import (
"wireguard-ui/config" "wireguard-ui/config"
"wireguard-ui/global/client" "wireguard-ui/global/client"
"wireguard-ui/global/constant" "wireguard-ui/global/constant"
"wireguard-ui/utils"
) )
// jwt密钥 // jwt密钥
@ -96,6 +97,16 @@ func (JwtComponent) ParseToken(token, secret string) (*JwtComponent, error) {
} }
} }
// GenerateSecret
// @description: 生成token解析密钥【每个用户的secret不一样提高安全性】
// @receiver JwtComponent
// @param secret
// @return string
func (JwtComponent) GenerateSecret(secret ...string) string {
secretStr := strings.Join(secret, ".")
return utils.Hash().MD5(utils.Hash().SHA256(utils.Hash().SHA512(secretStr)))
}
// Logout // Logout
// @description: 退出登陆 // @description: 退出登陆
// @receiver JwtComponent // @receiver JwtComponent

View File

@ -4,7 +4,9 @@ import (
"fmt" "fmt"
"gitee.ltd/lxh/logger/log" "gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/mojocn/base64Captcha" "github.com/mojocn/base64Captcha"
"time"
"wireguard-ui/component" "wireguard-ui/component"
"wireguard-ui/http/param" "wireguard-ui/http/param"
"wireguard-ui/http/response" "wireguard-ui/http/response"
@ -71,15 +73,16 @@ func (LoginApi) Login(c *gin.Context) {
return return
} }
secret := component.JWT().GenerateSecret(p.Password, uuid.NewString(), time.Now().Local().String())
// 生成token // 生成token
token, expireAt, err := component.JWT().GenerateToken(user.Id, utils.Hash().SHA256(p.Password)) token, expireAt, err := component.JWT().GenerateToken(user.Id, secret)
if err != nil { if err != nil {
log.Errorf("用户[%s]生成token失败: %v", user.Account, err.Error()) log.Errorf("用户[%s]生成token失败: %v", user.Account, err.Error())
response.R(c).FailedWithError("登陆失败!") response.R(c).FailedWithError("登陆失败!")
return return
} }
c.Writer.Header().Set("X-TOKEN", utils.Hash().SHA256(p.Password)) c.Writer.Header().Set("X-TOKEN", secret)
response.R(c).OkWithData(map[string]any{ response.R(c).OkWithData(map[string]any{
"token": token, "token": token,
"type": "Bearer", "type": "Bearer",