为什么Laravel 5.1种子';s哈希无法用于身份验证


Why Laravel 5.1 seed's hash is not working for Authentication

我制作了一个种子程序,用于让我的应用成为管理员用户

我的种子类

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作为所有测试的web服务器laravel版本5.1。很抱歉问了很长的问题

我从laracasts那里得到了答案。而不是对User模型进行散列处理。我们必须使用

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

在CCD_ 8模型中。我不知道为什么两个散列都不一样——这很好所以我的种子类会像这个

<?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();
        }
    }

您是否使用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'
            ] );
        }
    }