我从 Drupal 迁移时遇到了类似的问题。我没有为旧密码创建新列,而是更新了哈希以检查密码 Drupal-way,然后如果失败,请使用 bcrypt 进行检查。这样老用户就可以像新用户一样登录。
您需要在应用程序的任何位置创建一个包,例如在 app/packages/hashing 中。把这两个文件放在那里。
YourHashingServiceProvider.php
<?php namespace App\Packages\Hashing;
use Illuminate\Support\ServiceProvider;
class YourHashingServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('hash', function() { return new YourHasher; });
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['hash'];
}
}
YourHasher.php
<?php namespace App\Packages\Hashing;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Hashing\BcryptHasher;
use Auth;
class YourHasher implements HasherContract
{
protected $hasher;
/**
* Create a new Sha512 hasher instance.
*/
public function __construct()
{
$this->hasher = new BcryptHasher;
}
/**
* Hash the given value.
*
* @param string $value
* @param array $options
*
* @return string
*/
public function make($value, array $options = [])
{
return $this->hasher->make($value, $options);
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
*
* @return bool
*/
public function check($value, $hashedValue, array $options = [])
{
return md5($value) == $hashedValue || $this->hasher->check($value, $hashedValue, $options);
}
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
*
* @return bool
*/
public function needsRehash($hashedValue, array $options = [])
{
return substr($hashedValue, 0, 4) != '$2y$';
}
}
然后将App\Packages\Hashing\YourHashingServiceProvider::class 放入您的config/app.class 中的providers。此时,您的老用户应该可以登录您的 laravel 应用了。
现在,要更新他们的密码,您可以在用户控制器(登录/注册表单)的某个位置使用Hash::needsRehash($hashed) 和Hash::make($password_value) 为用户生成新的 bcrypt 密码,然后保存。