testfb/utils/jwt.go
2024-09-25 15:20:34 +08:00

27 lines
557 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"time"
"github.com/golang-jwt/jwt/v4"
)
// 密钥 签发JWT
var jwtSecret = []byte("mysecretkey")
// 模拟用户验证
func ValiddateUser(username, password string) bool {
return username == "admin" && password == "123456"
}
// 生成JWT
func GenerateJWT(username string) (string, error) {
// 设置过期时间
claims := jwt.MapClaims{
"username": username,
"exp": time.Now().Add(time.Hour * 72).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(jwtSecret)
}