laravel 在 lumen jwt 令牌中设置过期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36665489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set expire time in lumen jwt token
提问by bitcodr
I create an authentication api with jwt and Lumen.
我使用 jwt 和 Lumen 创建了一个身份验证 api。
I use tymondesigns/jwt-auth
package in my Lumen project for authentication. In project when users logon I want to expire user token after 1 month.
我tymondesigns/jwt-auth
在 Lumen 项目中使用包进行身份验证。在用户登录时的项目中,我想在 1 个月后使用户令牌过期。
Now how can i fix it?
现在我该如何解决?
回答by Angad Dubey
If you ran:
如果你跑了:
php artisan vendor:publish
php artisan vendor:publish
as per the installtion wiki: https://github.com/tymondesigns/jwt-auth/wiki/Installation
根据安装维基:https: //github.com/tymondesigns/jwt-auth/wiki/Installation
Then simple change the ttl
setting:
然后简单地更改ttl
设置:
// In config/jwt.php
...
/*
|--------------------------------------------------------------------------
| JWT time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token will be valid for.
| Defaults to 1 hour
|
*/
'ttl' => 43800, // valid for 1 month
...
回答by Tiago Gouvêa
In fact, to me, it just work when I changed the exp
parameter at JWT::encode
.
事实上,对我来说,当我exp
在JWT::encode
.
On my code, after use login I sent some response. Follow all my code. The exp
are on third method.
在我的代码中,使用登录后我发送了一些响应。遵循我的所有代码。这exp
是第三种方法。
/**
* Authenticate a user and return the token if the provided credentials are correct.
*
* @param Request $request
* @return mixed
* @internal param Model $user
*/
public function authenticate(Request $request)
{
$this->validate($this->request, [
'email' => 'required|email',
'password' => 'required'
]);
// Find the user by email
$user = User::where('email', $this->request->input('email'))->first();
if (!$user) {
return $this->responseError('USER_DOES_NOT_EXISTS', 404);
}
// Verify the password and generate the token
if (Hash::check($this->request->input('password'), $user->password)) {
return $this->responseUserData($user);
}
// Bad Request response
return $this->responseError('EMAIL_OR_PASSWORD_WRONG', 403);
}
/**
* Create response json
* @param $user
* @return \Illuminate\Http\JsonResponse
*/
private function responseUserData($user)
{
return response()->json([
'token' => $this->jwt($user),
'user' => $user->getUserData()
], 200);
}
/**
* Create a new token.
*
* @param \App\User $user
* @return string
*/
protected function jwt(User $user)
{
$payload = [
'iss' => "lumen-jwt", // Issuer of the token
'sub' => $user->id, // Subject of the token
'iat' => time(), // Time when JWT was issued.
'exp' => time() + 60 * 60 * 60 * 24 // Expiration time
];
// As you can see we are passing `JWT_SECRET` as the second parameter that will
// be used to decode the token in the future.
return JWT::encode($payload, env('JWT_SECRET'));
}
I wish it could help you.
我希望它可以帮助你。
回答by Fisherman
On latest(varsion >1.0.0) lumen JWT_TTL
in .env
will work as they use 'ttl' => env('JWT_TTL', 60),
in their internel code.
Ref: https://github.com/tymondesigns/jwt-auth/blob/develop/config/config.php
在最新的(varsion> 1.0.0)流明JWT_TTL
的.env
意愿工作,因为他们使用'ttl' => env('JWT_TTL', 60),
他们的个内码。
参考:https: //github.com/tymondesigns/jwt-auth/blob/develop/config/config.php
回答by Saddan
I'm using Lumen (5.8.12)
what I really did is set the value in .env
file like this
我正在使用Lumen (5.8.12)
我真正做的是在.env
文件中设置这样的值
Just add the value of JWT_TTL
in your .env
file.The default time is 60 minutes here my value represent 1440(60*24) minute or 1 day
只需JWT_TTL
在您的.env
文件中添加值。默认时间是 60 分钟这里我的值代表 1440(60*24) 分钟或 1 天