por*_*aus 3 go jwt claims-authentication
我正在使用此包github.com/dgrijalva/jwt-go/v4在登录函数中设置声明:
now := time.Now()
claims := &jwt.StandardClaims{
Issuer: "Test",
ExpiresAt: now.Add(time.Hour * 24).Unix(),
}
Run Code Online (Sandbox Code Playgroud)
IDE 不断告诉我:
无法使用 'now.Add(time.Hour * 24).Unix()' (类型 int64)作为Time类型。
我读到,因为我输入了错误的值,但是,在我在网上看到的所有示例中,这正是大多数人的设置方式。
我仍在学习 go,所以我不确定将此时间格式转换为有效格式的正确方法。
在 github.com/golang-jwt/jwt/v4 中,StandardClaims 类型已被弃用,您应该将 StandardClaims 替换为 RegisteredClaims。
关于Cannot use 'now.Add(time.Hour * 24).Unix()' (type int64) as the type Time.您需要使用 NumericDate 类型,因此您的代码将如下所示:
claims := &jwt.RegisteredClaims{
Issuer: "Test",
ExpiresAt: &jwt.NumericDate{now.Add(time.Hour * 24)},
}
Run Code Online (Sandbox Code Playgroud)
小智 5
好的,你可以改变
github.com/dgrijalva/jwt-go/v4 => github.com/golang-jwt/jwt/v4 //v4.4.3
StandardClaims => RegisteredClaims
now.Add(time.Hour * 24).Unix() => jwt.NewNumericDate(now.Add(time.Hour * 24))
Run Code Online (Sandbox Code Playgroud)