【问题标题】:Laravel Hash::make() and Hash::check() not working with PostgresLaravel Hash::make() 和 Hash::check() 不适用于 Postgres
【发布时间】:2016-04-28 17:16:14
【问题描述】:

我对 Laravel 5、Postgres 和 Hash 外观有一个奇怪的问题。我有以下测试代码:

public static function addNewPartner($args){
    $user = new User;
    $user->username = $args['username'];
    $user->email = $args['email'];
    // Here starts the issue
    $password = Hash::make($args['password']);
    $user->password = $password;
    $user->remote_key = str_random(16);
    if(!$user->save()){
        return false;
    }
    $role = Role::where('name','=','partner')->first();
    $user->attachRole($role);
    Log::info("User::addNewPartner. User:" . json_encode($user));

    // At this point the password is hashed and saved into DB, I can
    // see the record saved. 

    // I compare the saved password with the input and it fails. 
    if(Hash::check($args['password'], $user->password)){
        Log::info('DB hash' . " " . $password);
    } else {
        Log::info('DB no hash' . " " . $password);
    }

    // I compare the password as it was generated with the input and 
    // it passes. 
    if(Hash::check($args['password'], $password)){
        Log::info('LOCAL hash' . " " . $password);
    } else {
        Log::info('LOCAL no hash' . " " . $password);
    }


    return $user;
}

日志输出为:

[2016-04-28 12:04:46] local.INFO: UserController::postAddnewpartner。 Input:{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6XC9cL3Vici5sb2NhbDo4MDAwXC9hcGlcL3VzZXJcL2xvZ2luIiwiaWF0IjoxNDYxODYyNDYwLCJleHAiOjE0NjE4NjYwNjAsIm5iZiI6MTQ2MTg2MjQ2MCwianRpIjoiZmU0MTgzMTAwMzQyMjU3Mjg2MGJmOWJhN2FmOWIyMDMifQ.iUZzepZshdn7snUGOzTt3AEAyfRKNGPrCJpk5FxJtxw","fullname":"usuario1","email":"usuario1@usuario.com","password":"1234","username":"usuario1" }

[2016-04-28 12:04:46] local.INFO: User::addNewPartner。用户:{"username":"usuario1","email":"usuario1@usuario.com","re​​mote_key":"jhDpJgolw4bTdsZj","updated_at":"2016-04-28 12:04:46","created_at ":"2016-04-28 12:04:46","id":6}

[2016-04-28 12:04:46] local.INFO: DB 无哈希 $2y$10$PU24f.AkNguifnh1AkKSJuVu7I4idWGUz8SA2L/37sRsI6JaVkQZC

[2016-04-28 12:04:46] local.INFO: 本地哈希 $2y$10$PU24f.AkNguifnh1AkKSJuVu7I4idWGUz8SA2L/37sRsI6JaVkQZC

由于某种原因,如果从数据库中检索记录,则 Hash::check() 不起作用。

奇怪的是,在播种过程中创建的测试用户工作:

class UserTableSeeder extends Seeder {

public function run() {
    $users = array(
        array(
            'username' => 'admin',
            'email' => 'admin@example.org',
            'password' => Hash::make('1234'),
            'parent_id'=>null,
            'created_at' => new DateTime,
            'updated_at' => new DateTime,
        ),

我的用户迁移是:

public function up() {
    Schema::create('users', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('parent_id')->unsigned()->nullable();
        $table->string('username')->unique();
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('remote_key')->default(str_random(16));
        $table->rememberToken();
        $table->timestamps();
    });
}

你知道哈希失败的原因吗?谢谢。

【问题讨论】:

    标签: postgresql laravel hash


    【解决方案1】:

    所以问题正是在这些方面:

    $password = Hash::make($args['password']);
    // more code
    if(!$user->save()){
        return false;
    }
    

    在保存用户的那一刻,密码被散列,因此程序正在散列散列。所以,这些行:

    // I compare the saved password with the input and it fails. 
    if(Hash::check($args['password'], $user->password)){
        Log::info('DB hash' . " " . $password);
    } else {
        Log::info('DB no hash' . " " . $password);
    }
    

    实际上是将存储在数据库中的散列哈希与散列密码进行比较,结果比较返回错误。

    【讨论】:

      猜你喜欢
      • 2014-08-31
      • 2014-03-11
      • 2021-05-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 2020-02-14
      • 2021-06-05
      相关资源
      最近更新 更多