【问题标题】:Why Laravel 5.1 seed's hash is not working for Authentication为什么 Laravel 5.1 种子的哈希不适用于身份验证
【发布时间】:2015-06-25 19:43:39
【问题描述】:

我制作了一个播种机,用于为我的应用创建管理员用户

我的种子类

use Illuminate\Database\Seeder;
use App\User;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        // $this->call('UserTableSeeder');
        User::create([
        'email'=>'admin',
        'username'=>'admin',
        'password'=>bcrypt('admin'),
        'name'=>'admin',
        'type'=>'a',
        'lastLogin'=>'test',
        'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1']);

        Model::reguard();
    }
}

它正在我的数据库中创建记录。但是,如果我将此记录用于AuthController,它会失败。所以尝试像这样使用route.php闭包创建记录

Route::get('install',function(){


    return \App\User::create([
        'email'=>'admin',
        'username'=>'admin',
        'password'=>bcrypt('admin'),
        'name'=>'admin',
        'type'=>'a',
        'lastLogin'=>'test',
        'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1']);

});

这对AuthController 非常有效。因此,为了找出问题,我尝试用这个哈希替换种子创建记录的哈希。然后它运作良好。为什么 bcrypt() 哈希都不同,即使两者都使用相同的随机字符串作为 salt ?

用户表迁移

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{   
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');

            $table->string('username'); 
            $table->string('name');
            $table->string('email');
            $table->string('password', 60);
            $table->enum('type',['a','m','u']);
            $table->string('lastLogin');
            $table->string('permission');

            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}

.env 文件

APP_ENV=local
APP_DEBUG=true
APP_KEY=aL3s6hAk375ogGSJQVKVB1r3Jf6OHZ5j

DB_HOST=localhost
DB_DATABASE=mymoney
DB_USERNAME=root
DB_PASSWORD=vip12340

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

config\app.php

return [

    'debug' => env('APP_DEBUG'),

    'url' => 'http://localhost',

    'timezone' => 'UTC',

    'locale' => 'en',

    'fallback_locale' => 'en',

    // I tried both ways
    // 'key' => env('APP_KEY', 'SomeRandomString'), 
    'key' => env('APP_KEY', 'aL3s6hAk375ogGSJQVKVB1r3Jf6OHZ5j'),

    'cipher' => 'AES-256-CBC',

    'log' => 'single',

   // All default providers and aliases 

];

我使用php artisan serv 作为所有测试的网络服务器laravel 5.1版。对不起,很长的问题

【问题讨论】:

    标签: php hash laravel-5 seeding


    【解决方案1】:

    我从laracasts 得到了答案。而不是在User 模型中使用哈希。我们必须使用

    Public function setPasswordAttribute($password) 
    { 
             return $this->attributes['password'] = bcrypt($password); 
    }
    

    User 模型中。我不知道为什么两个哈希不同,这很好用 所以我的种子类会是这样的

    <?php
    
        use Illuminate\Database\Seeder;
        use App\User;
        use Illuminate\Database\Eloquent\Model;
    
        class DatabaseSeeder extends Seeder
        {
            /**
             * Run the database seeds.
             *
             * @return void
             */
            public function run()
            {
                Model::unguard();
    
                // $this->call('UserTableSeeder');
                User::create([
                'email'=>'admin',
                'username'=>'admin',
                'password'=>'admin',
                'name'=>'admin',
                'type'=>'a',
                'lastLogin'=>'test',
                'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1']);
    
                Model::reguard();
            }
        }
    

    【讨论】:

      【解决方案2】:

      你使用的是 Laravel 默认的 Auth 系统吗?如果是,那么您应该输入您的有效电子邮件以使登录成功。你可以这样试试。

      <?php
          use Illuminate\Database\Seeder;
          use Illuminate\Database\Eloquent\Model;
          class DatabaseSeeder extends Seeder {
              /**
               * Run the database seeds.
               *
               * @return void
               */
              public function run()
              {
                  Model::unguard();
                  $this->call('UserTableSeeder');
              }
          }
          class UserTableSeeder extends Seeder {
              /**
               * Run the database seeds.
               *
               * @return void
               */
              public function run() {
                  App\User::create( [
                      'username' => 'admin'
                      'email' => 'admin@gmail.com',
                      'password' => Hash::make( 'password' ),
                      'name' => 'Admin ',
                      'type'=>'a',
                      'lastLogin'=>'test',
                      'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1'
                  ] );
              }
          }
      

      【讨论】:

      • 我尝试使用Hash::make() 仍然是同样的问题。电子邮件是正确的,事实上,如果我替换散列,它会很好地工作
      • 你可以使用 bcrypt() 或 Hash::make() 任何一个。现在有什么问题?
      • 确切的错误是什么?你的播种成功了吗?
      • 是的,播种成功,但如果我使用该记录,Auth 将返回 false。如果我用route.php 中生成的散列更新散列,同样的事情会起作用
      • 您是否对 Auth 控制器进行了任何更改?
      【解决方案3】:

      这篇文章解释了这个问题并有一个实际可行的解决方案:

      https://scotch.io/tutorials/simple-and-easy-laravel-login-authentication

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        • 2022-12-15
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        相关资源
        最近更新 更多