【问题标题】:Hash::check Always FailingHash::check 总是失败
【发布时间】:2014-09-05 07:37:20
【问题描述】:

我在 Linux 和 Windows 上使用 Auth 完成了项目,从未遇到过哈希问题。它的基本要点是密码不通过默认用户设置的 Hash::check 方法。我已经查看了关于 SO 的大多数答案,他们是不理解 Hash 每次都生成不同值的典型用户,或者 Hash::check 需要普通值,然后是数据库中用户的哈希值。起初这是 Auth::attempt 的失败,但进一步看似乎是哈希验证问题。我已经阅读并重新阅读 Laravel Authentication 以确保我没有错过诸如确保 db 密码字段太短(设置为 256 的长度只是为了确定)之类的东西。

设置

用户模型

class User extends Eloquent implements UserInterface, RemindableInterface {
  protected $fillable = array(
    'email',
    'password',
  );

  protected $guarded = array(
  'id',
  );

  //add the UserInterface / RemindableInterface methods here
}

种子

User::create(array( //could also do DB insert way..
  'id'=>1,
  'email' => 'admin@example.com',
  'password' => Hash::make('admin'),
));

//User created with correct hash no problem.

测试发布路线

//some other validation above this. Email / password correctly passed from form
$email = Input::get('email');
$password = Input::get('password');

$user = User::where('email', $email)->first();
//gets user with hashed pass fine
$sucess = Hash::check( $password, $user->password ); //could do $user->getAuthPassword() as well
//always false..
//initially tried Auth::attempt getting the same results because of the internal Hash::check

试过

  • 我已逐步完成验证,将带盐的 db 哈希与插入的普通通道进行比较,在盐比较后失败。
  • 我尝试在获取登录路径(如下)上创建一个新用户并尝试在表单上使用这些凭据,但它仍然无法登录(认为这可能是种子问题。)

    $newUser = 新用户(数组( '电子邮件'=>'admin2@example.com', '密码'=>哈希::make('test') )); $newUser->save();

在这一点上,我不知道这似乎是一个奇怪的密码验证/密码哈希问题。

【问题讨论】:

  • 如果你Hash::check('admin', Hash::make('admin'))会发生什么?
  • 效果很好(返回真)。在这一切之后,我本以为会失败的情况之一,但仍然让我大吃一惊。
  • 那么Input::get('password') 可能不是您认为的那样,或者$user->password 不是。你有关于电子邮件的唯一索引吗?没有使用不同密码的重复电子邮件?模型中是否有 creating 侦听器第二次对密码进行哈希处理?
  • 原来有一个变异器,我必须更深入地挖掘。不过感谢您的快速审核。

标签: laravel-4


【解决方案1】:

原来在用户模型上设置了一个默认的 setPasswordAttribute 方法,该方法通过起始填充器为任何创建或构造的用户运行。

删除:

public function setPasswordAttribute( $value )
{
  $this->attributes['password'] = Hash::make( $value );
}

而且它工作正常。

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 2019-08-15
    • 2014-02-25
    • 2021-06-05
    • 2020-02-14
    • 1970-01-01
    • 2015-02-18
    • 2014-11-19
    • 1970-01-01
    相关资源
    最近更新 更多