laravel 没有remember_token的Laravel 5身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43467328/
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
Laravel 5 authentication without remember_token
提问by Willem
I am using an existing database, and I'm not allowed to modify the tables, so adding a remember_token is not an option, but without it I'm unable to login. When I try to login Laravel does check the credentials and returns whether they match the records, but it only refreshes the page. I am pretty sure the remember_token is the cause since I've encountered this problem before, but this time I can't add a column to my users table.
我正在使用现有的数据库,我不允许修改表,所以添加一个 remember_token 不是一个选项,但没有它我无法登录。当我尝试登录 Laravel 时,它会检查凭据并返回它们是否与记录匹配,但它只会刷新页面。我很确定 remember_token 是原因,因为我以前遇到过这个问题,但这次我无法向我的用户表中添加一列。
Is there a way to use the out-of-the-box authentication without the remember_token?
有没有办法在没有 remember_token 的情况下使用开箱即用的身份验证?
回答by Autista_z
In your User model add:
在您的用户模型中添加:
/**
* Overrides the method to ignore the remember token.
*/
public function setAttribute($key, $value)
{
$isRememberTokenAttribute = $key == $this->getRememberTokenName();
if (!$isRememberTokenAttribute)
{
parent::setAttribute($key, $value);
}
}
Credits: https://laravel.io/forum/05-21-2014-how-to-disable-remember-token
学分:https: //laravel.io/forum/05-21-2014-how-to-disable-remember-token
回答by Roland Starke
Since Laravel v5.3.27 you can also disable the remember me functionality by setting the $rememberTokenName
to false
in your User model.
从 Laravel v5.3.27 开始,您还可以通过在 User 模型中设置$rememberTokenName
to 来禁用记住我的功能false
。
class User extends Authenticatable
{
use Notifiable;
protected $rememberTokenName = false;
// ...
}
source: this commit
来源:这次提交
回答by Mark Baaijens
In order to really disable the "remember me" functionality and to be sure the remember_token field is not used, add the following code to the boot method of App\Providers\AuthServiceProvider.
为了真正禁用“记住我”功能并确保不使用 remember_token 字段,请将以下代码添加到 App\Providers\AuthServiceProvider 的启动方法中。
Auth::provider('eloquent', function($app, array $config)
{
return new class($app['hash'], $config['model']) extends \Illuminate\Auth\EloquentUserProvider
{
public function retrieveByToken($identifier, $token)
{
return null;
}
public function updateRememberToken(\Illuminate\Contracts\Auth\Authenticatable $user, $token)
{
//Do nothing
}
};
});
assuming a default Laravel setup, the code above will work instantly.
假设默认 Laravel 设置,上面的代码将立即生效。
Off course, it can be improved by defining the custom user provider in a seperate file instead of using a anonymous class.
当然,它可以通过在单独的文件中定义自定义用户提供程序而不是使用匿名类来改进。