完善cookie模式
This commit is contained in:
+25
-1
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user