package pkg import ( "fmt" "github.com/golang-jwt/jwt/v4" "hilo/app/service/user/internal/conf" "time" ) // Claims 载荷,增加用户别名 type Claims struct { UserId int64 ExternalId string jwt.StandardClaims } func GenerateJwtToken(userId int64, externalId string) (string, error) { jwtConfig := conf.Conf.Auth.Jwt duration, err := time.ParseDuration(jwtConfig.Expire) if err != nil { return "", fmt.Errorf("jwt parse time err: %s", err) } expireTime := time.Now().Add(duration) claims := Claims{ UserId: userId, ExternalId: externalId, StandardClaims: jwt.StandardClaims{ ExpiresAt: expireTime.Unix(), //过期时间 Issuer: jwtConfig.Issuer, //签名的发行者 }, } tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token, err := tokenClaims.SignedString([]byte(jwtConfig.Secret)) return token, err }