php 动态设置laravel jwt的到期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41141063/
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 expiry time for laravel jwt dynamically
提问by user254153
Hi I am using angular js in front end with satellizer and laravel at backend with tymon jwt library. I am using jwt authentication. I want to make remember me functionalities in my web app. I see 'ttl' to set expiry time of token in laravel 'config/jwt.php.
嗨,我在前端使用 angular js,使用 satellizer,后端使用 laravel 使用 tymon jwt 库。我正在使用 jwt 身份验证。我想让我的网络应用程序记住我的功能。我看到 'ttl' 在 laravel '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' => 60,
By default, it will be 1 hour. But I want to change this dynamically to 1 week if user clicks remember me while login. How can I change it dynamically. Thank you.
默认情况下,它将是 1 小时。但是如果用户在登录时点击记住我,我想将其动态更改为 1 周。我怎样才能动态改变它。谢谢你。
回答by Jamesking56
You can add exp
as a custom claim as follows:
您可以添加exp
为自定义声明,如下所示:
$token = JWTAuth::attempt($credentials, ['exp' => Carbon\Carbon::now()->addDays(7)->timestamp]);
The code above creates a token that expires in 7 days time. You don't have to use Carbon
it just requires a Unix timestamp, I've used Carbon
here for simplicity since its built into Laravel.
上面的代码创建了一个在 7 天后过期的令牌。你不必使用Carbon
它只需要一个 Unix 时间戳,我在Carbon
这里使用它是为了简单起见,因为它内置于 Laravel 中。
回答by Andrey Lutskevich
You can use JWTFactory
(1.0 version)
您可以使用JWTFactory
(1.0版本)
$myTTL = 30; //minutes
JWTAuth::factory()->setTTL($muTTL);
$token = JWTAuth::attempt($credentials);
回答by Yoram de Langen
I'm not 100% sure, but what happens if you set within your AppServiceProvider@register
the config:
我不是 100% 确定,但是如果您AppServiceProvider@register
在配置中进行设置会发生什么:
config()->set('jwt.ttl', 60*60*7);
or with a facade:
或带有立面:
Config::set('jwt.ttl', 60*60*7);
Why would you set it dynamically? Or do you not use the publishing from the config (it's not publishing the config/jwt.php
)?
为什么要动态设置?或者您不使用配置中的发布(它不发布config/jwt.php
)?
EDIT:
编辑:
Another solution would be to set it through your .env
file:
另一种解决方案是通过您的.env
文件设置它:
config/jwt.php
// set the default TTL to one week if the .env file does not contain a `JWT_TTL` var
'ttl' => env('JWT_TTL', 60*60*7),
And within .env
:
并且在.env
:
JWT_TTL=3600
回答by Mahmoud Ali Kassem
Tymon JWT v 1.0
泰蒙 JWT v 1.0
you can override default ttl when attempting to login user:
您可以在尝试登录用户时覆盖默认 ttl:
if (! $token = auth()->setTTL(1)->attempt($credentials)) {
return response()->json(['message' => 'Unauthorized user'], 401);
}
回答by Sulung Nugroho
For JWT version 1.0.0-rc.2 it's very clear described on the documentation on config/jwt.php
对于 JWT 版本 1.0.0-rc.2,在 config/jwt.php 的文档中描述得非常清楚
As per note : .... You can also set this to null, to yield a never expiring token. Some people may want this behaviour for e.g. a mobile app. This is not particularly recommended, so make sure you have appropriate systems in place to revoke the token if necessary. Notice:If you set this to null you should remove 'exp' elementfrom 'required_claims' list.
根据注释: .... 您还可以将其设置为 null,以生成永不过期的令牌。有些人可能希望这种行为用于例如移动应用程序。这不是特别推荐,因此请确保您有适当的系统来在必要时撤销令牌。 注意:如果您将此设置为 null,则应从“required_claims”列表中删除“exp”元素。
'ttl' => env('JWT_TTL', 60) meaning we must set 60 to null
'required_claims' => [
'iss',
'iat',
// 'exp', <- remove this
'nbf',
'sub',
'jti',
],
回答by joel kithinji
None of the above answers worked for me. I managed to get it working like this.
以上答案都不适合我。我设法让它像这样工作。
$ttl_in_minutes = 60*24*100;
// The parameter passed to the auth helper should match what is present in config/auth.php
if($request->input('remember')) auth('api')->factory()->setTTL($ttl_in_minutes);
回答by Lalit Baghel
Increase Laravel auth token expire time
增加 Laravel 身份验证令牌过期时间
SESSION_LIFETIME=10080
Default value 120 min in session.php
session.php 中的默认值 120 分钟
回答by Vedmant
You can do following to generate JWT token with needed expire time:
您可以执行以下操作来生成具有所需过期时间的 JWT 令牌:
JWTAuth::customClaims(['exp' => Carbon\Carbon::now()->addDays(2)->timestamp])
->fromUser($user);
回答by Prakash P
We can set token expiry time while creating the JWT token . It can be set in the token parameter. For example
我们可以在创建 JWT 令牌时设置令牌到期时间。可以在token参数中设置。例如
$token = array(
"iss" => "http://example.com",
"aud" => "http://example.com",
"exp" => {YOUR_EXPIRY_TIME}
);
$jwt=new JWT();
$JWT_TOKEN=$jwt->encode($token, {YOUR_KEY});
The new token will be generated with the corresponding expiry time.
新令牌将在相应的到期时间生成。