package util
|
|
import (
|
"plc-recorder/config"
|
"time"
|
|
"github.com/golang-jwt/jwt/v4"
|
)
|
|
type CustomClaims struct {
|
BaseClaims
|
jwt.StandardClaims
|
}
|
|
type BaseClaims struct {
|
UserId string
|
Username string
|
ParentId string
|
}
|
|
func CreateToken() (string, error) {
|
baseClaims := BaseClaims{
|
UserId: "plc-recorder",
|
Username: "plc-recorder",
|
ParentId: config.Options.ParentId,
|
}
|
claims := CustomClaims{
|
BaseClaims: baseClaims,
|
StandardClaims: jwt.StandardClaims{
|
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
|
ExpiresAt: time.Now().Add(1 * time.Minute).Unix(), // 过期时间
|
Issuer: "plc-recorder", // 签名的发行者
|
},
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
signKey := []byte(config.Options.JWTKey)
|
|
return token.SignedString(signKey)
|
}
|