dongyan5141 2018-04-05 11:09
浏览 122

使用Crypt :: encrypt方法后,Laravel身份验证失败

I am using Laravel 5.4.

Recently, I have had a requirement to encrypt the data of users table. So that, I have followed [this][1] link, and created an Encryptable trait as,

<?php
namespace App\Traits;
use Config;
use Crypt;

trait Encryptable
{
    public function getAttribute($key)
    {
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable) && Config::get('app.encrypt_data_at_rest')) {
            $value = Crypt::decrypt($value);
        }
        return $value;
    }

    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encryptable) && Config::get('app.encrypt_data_at_rest')) {
            $value = Crypt::encrypt($value);
        }
        return parent::setAttribute($key, $value);
    }
}

and used it in my User model like;

<?php
namespace App\Models;
class User extends BaseModel implements AuthenticatableContract, 
                                         AuthorizableContract,
                                         CanResetPasswordContract  {

    use SoftDeletes;
    use Encryptable;

    protected $encryptable = ['username'];

And inserted a record and the username encrypted successfully.

But now, the issue is, I am not able to login using this username and password.

In the database, the username seems encrypted. If I get that specific user's username in my controller, (e.g. User::find(89)->username) then it returns me the decrypted username (i.e. simple string that I have inserted while creating this user) due to getAttribute() method of Encryptable trait.

But however I am not able to use Auth::attempt() method as it returns false. I am passing the non-encrypted username and password while using Auth::attempt() method.

Any suggestion how to fix this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于这个问题的代码运行
    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败