简体   繁体   English

尝试进行身份验证时JWT令牌已过期?

[英]Expired JWT Token when trying to authenticate?

I am using JWT for the first time, I am finding a hard time to understand the way it works fully, every time I find a solution to simethong, an other thing pops out. 我是第一次使用JWT,每当我找到四美通的解决方案时,我都很难理解它的全部工作方式,但又出现了另一件事。

I was trying to authenticate before and when I send the correct username and password I get a token when using Postman. 我在发送正确的用户名和密码之前和之后进行身份验证时,使用Postman时获得了令牌。

I left the project few hours and came back and now when I try to authenticate I get this error : 我离开了项目几个小时,然后又回来了,现在当我尝试进行身份验证时,出现此错误:

{
    "code": 401,
    "message": "Expired JWT Token"
}

I don't know how to solve it, because I did not knwo I may face such problem. 我不知道如何解决,因为我不知道我可能会遇到这样的问题。

Any help would be much appreciated. 任何帮助将非常感激。

It goes like this: 它是这样的:

You make first api request, pass your credentials, get token and store it somewhere. 您发出第一个api请求,传递您的凭据,获取令牌并将其存储在某处。

After that you should pass that token (trough "Bearer" header field) with every other request so server could identify you. 之后,您应该将令牌(通过“ Bearer”标头字段通过)与其他所有请求一起传递,以便服务器可以识别您。

But that token won't be valid forever. 但是该令牌不会永远有效。 After some time (TTL) it will expire and you'll get message like that. 一段时间(TTL)之后,它将过期,您将收到类似的消息。 Then you have to repeat authentication to get new token. 然后,您必须重复身份验证才能获取新令牌。

So just repeat request you made at start, pass again credentials, collect and store again new token and keep using it as before. 因此,只需重复您在开始时提出的请求,再次传递凭据,再次收集和存储新令牌并像以前一样继续使用它。

If you're using LexikJWTAuthenticationBundle , you can set the TimeToLive for JWT in lexik_jwt_authentication.yaml in config/packages. 如果使用LexikJWTAuthenticationBundle ,则可以在config / packages中的lexik_jwt_authentication.yaml中为JWT设置TimeToLive

The token_ttl: attribute is for setting up the TimeToLive of a token in seconds (eg, 604800 is equal to 7 days). token_ttl:属性用于以秒为单位设置令牌的TimeToLive (例如604800等于7天)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM