parent
7d70ba4236
commit
4af62b27cb
@ -0,0 +1,36 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/zggsong/gin-vue-demo/model"
|
||||
)
|
||||
|
||||
var jwtKey = []byte("a_secret_key")
|
||||
|
||||
type Claims struct {
|
||||
UserId uint
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
func ReleaseToken(user model.User) (string, error) {
|
||||
// 设置Token过期时间
|
||||
expirationTime := time.Now().Add(7 * 24 * time.Hour)
|
||||
claims := Claims{
|
||||
UserId: user.ID,
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
ExpiresAt: expirationTime.Unix(),
|
||||
IssuedAt: time.Now().Unix(),
|
||||
Issuer: "github.com/zggsong/gin-vue-demo",
|
||||
Subject: "user token",
|
||||
},
|
||||
}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString(jwtKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tokenString, nil
|
||||
}
|
Loading…
Reference in new issue