Golang jwt.StandardClaims 时间格式类型问题

huangapple go评论104阅读模式
英文:

Golang jwt.StandardClaims time format type issue

问题

我正在使用github.com/dgrijalva/jwt-go/v4这个包来设置登录函数中的声明:

now := time.Now()
claims := &jwt.StandardClaims{
    Issuer:    "Test",
    ExpiresAt: now.Add(time.Hour * 24).Unix(),
}

我的IDE一直告诉我:

> 无法将'now.Add(time.Hour * 24).Unix()'(类型为int64)用作Time类型。

我看到这个错误是因为我使用了错误的类型,然而,在我看过的所有在线示例中,这正是大多数人设置的方式。

我还在学习Go语言,所以我不确定将这个时间格式转换为有效格式的正确方法是什么。

英文:

I am using this package github.com/dgrijalva/jwt-go/v4 to set up claims in a Login function:

now := time.Now()
claims := &jwt.StandardClaims{
	Issuer: "Test",
	ExpiresAt: now.Add(time.Hour * 24).Unix(),
}

The IDE keeps telling me:

> Cannot use 'now.Add(time.Hour * 24).Unix()' (type int64) as the type Time.

I read that as I have the wrong typed value, however, on all the examples I've seen online, this is exactly how the majority set up this up.

I am still learning go and so I am not sure the proper way to convert this time format into something that is valid.

答案1

得分: 2

好的,以下是翻译好的内容:

好的,你可以更改:

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))
英文:

Okay, you can change

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))

答案2

得分: 1

ExpiresAt要求数据类型为*time.Time,而函数Unix()int64的形式返回时间的秒数。

我建议你使用github.com/golang-jwt/jwt这个包,而不是你现在使用的那个,因为后者已经不再维护了。

英文:

ExpiresAt requires the datatype to be *time.Time and the function Unix() returns the time in a number of seconds in int64.

I recommend you to use the package github.com/golang-jwt/jwt rather than the one you are using now, which is no longer maintained.

答案3

得分: 1

在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{Time: now.Add(time.Hour * 24)},
}
英文:

In github.com/golang-jwt/jwt/v4 StandardClaims type is deprecated, you should replace StandardClaims with RegisteredClaims.

And about Cannot use 'now.Add(time.Hour * 24).Unix()' (type int64) as the type Time. you need to use NumericDate type, so your code will looks like this:

claims := &jwt.RegisteredClaims{
    Issuer: "Test",
    ExpiresAt: &jwt.NumericDate{now.Add(time.Hour * 24)},
}

答案4

得分: 0

func GenerateToken(username, password string) (string, error) {
	nowTime := time.Now()
	expireTime := nowTime.Add(12 * time.Hour)

	claims := Claims{
		username,
		password,
		jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(expireTime),
			Issuer:    "test",
		},
	}
	
	tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	token, err := tokenClaims.SignedString(jwtSecret)

	return token, err
}

你可以试一下。

英文:
func GenerateToken(username, password string) (string, error) {
	nowTime := time.Now()
	expireTime := nowTime.Add(12 * time.Hour)

	claims := Claims{
		username,
		password,
		jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(expireTime),
			Issuer:    "test",
		},
	}
	
	tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	token, err := tokenClaims.SignedString(jwtSecret)

	return token, err
}

You can try it

答案5

得分: 0

你的代码没问题,问题出在导入包的部分。你可以将导入语句中的

"github.com/dgrijalva/jwt-go/v4"

改为

"github.com/dgrijalva/jwt-go"
英文:

your code is Okay the problem is with the importation of your package you can change the import

from

"github.com/dgrijalva/jwt-go/v4"

to

"github.com/dgrijalva/jwt-go"

huangapple
  • 本文由 发表于 2022年2月15日 06:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/71119129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定