为什么 Laravel 5.1 种子的哈希不适用于身份验证
Why Laravel 5.1 seed's hash is not working for Authentication
我制作了一个播种器,用于为我的应用创建管理员用户
我的种子class
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 相同的随机字符串?
用户迁移table
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 版 。对不起,很长的问题
您使用的是 Laravel 默认身份验证系统吗?如果是,那么您应该输入您的有效电子邮件以使登录成功。你可以这样试试。
<?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'
] );
}
}
我从laracasts那里得到了答案。而不是使散列成为我们 User
模型的一方。我们必须使用
Public function setPasswordAttribute($password)
{
return $this->attributes['password'] = bcrypt($password);
}
在 User
模型中。我不知道为什么两个散列不同这很好用
所以我的种子 class 会像这样
<?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();
}
}
这篇文章解释了这个问题并提供了一个实际有效的解决方案:
https://scotch.io/tutorials/simple-and-easy-laravel-login-authentication
我制作了一个播种器,用于为我的应用创建管理员用户
我的种子class
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 相同的随机字符串?
用户迁移table
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 版 。对不起,很长的问题
您使用的是 Laravel 默认身份验证系统吗?如果是,那么您应该输入您的有效电子邮件以使登录成功。你可以这样试试。
<?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'
] );
}
}
我从laracasts那里得到了答案。而不是使散列成为我们 User
模型的一方。我们必须使用
Public function setPasswordAttribute($password)
{
return $this->attributes['password'] = bcrypt($password);
}
在 User
模型中。我不知道为什么两个散列不同这很好用
所以我的种子 class 会像这样
<?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();
}
}
这篇文章解释了这个问题并提供了一个实际有效的解决方案:
https://scotch.io/tutorials/simple-and-easy-laravel-login-authentication