完善cookie模式

This commit is contained in:
piexlMax
2024-01-04 22:15:54 +08:00
parent 8aefb5db8e
commit 08617f2fd4
7 changed files with 40 additions and 31 deletions
+25 -1
View File
@@ -5,13 +5,37 @@ import (
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid/v5"
"net"
)
func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
func ClearToken(c *gin.Context) {
// 增加cookie x-token 向来源的web添加
host, _, err := net.SplitHostPort(c.Request.Host)
if err != nil {
host = c.Request.Host
}
c.SetCookie("x-token", "", -1, "/", host, false, false)
}
func SetToken(c *gin.Context, token string, maxAge int) {
// 增加cookie x-token 向来源的web添加
host, _, err := net.SplitHostPort(c.Request.Host)
if err != nil {
host = c.Request.Host
}
c.SetCookie("x-token", token, maxAge, "/", host, false, false)
}
func GetToken(c *gin.Context) string {
token, _ := c.Cookie("x-token")
if token == "" {
token = c.Request.Header.Get("x-token")
}
return token
}
func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
token := GetToken(c)
j := NewJWT()
claims, err := j.ParseToken(token)
if err != nil {