🎨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/global/client"
"wireguard-ui/global/constant"
"wireguard-ui/utils"
)
// 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
// @description: 退出登陆
// @receiver JwtComponent

View File

@ -4,7 +4,9 @@ import (
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/mojocn/base64Captcha"
"time"
"wireguard-ui/component"
"wireguard-ui/http/param"
"wireguard-ui/http/response"
@ -71,15 +73,16 @@ func (LoginApi) Login(c *gin.Context) {
return
}
secret := component.JWT().GenerateSecret(p.Password, uuid.NewString(), time.Now().Local().String())
// 生成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 {
log.Errorf("用户[%s]生成token失败: %v", user.Account, err.Error())
response.R(c).FailedWithError("登陆失败!")
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{
"token": token,
"type": "Bearer",