【问题标题】:How to validate bcrypt password in Laravel?如何在 Laravel 中验证 bcrypt 密码?
【发布时间】:2018-06-30 01:49:54
【问题描述】:

我有一个更改密码表单,其中包含旧密码和新密码字段 旧密码字段名为old_password

在我的控制器中,我正在验证这样的密码

$this->validate($request, [
            'old_password' => 'required|min:6|exists:users,password',
            'password' => 'required|min:6:max:30',
]); 

但是当我将old_password直接与没有功能bcrypt()的密码进行比较时,它不起作用。

那么我如何将old_password 字段与用户字段中已经存储的 bcrypted 密码进行比较。

注意:我想返回验证错误,密码没有 如果不同则匹配

【问题讨论】:

  • 您需要为此使用自定义验证规则或扩展验证器

标签: php laravel validation laravel-5


【解决方案1】:

您需要比较old_password 和您要比较的其他密码的哈希结果。

// when old_password is not encrypted but other_password is:
bcrypt('old_password') === hash_of_other_password

使用$this->validate 而不编写自定义规则的一种方法是使用$request->merge()。这样你就可以使用confirmed 规则,如果你的数据库中有散列值,你甚至可以使用exists

【讨论】:

  • 虽然这可能行得通,但这不是一个好的做法,因为它可能允许定时攻击来帮助确定所使用的哈希算法的类型。
【解决方案2】:

这就是我将如何制作这样的功能

HTML 表单

<form action="{{ url('admin/admin-admin-password-update/'.$admin->id) }}" method="post">
    {{ csrf_field() }}
    <div class="form-group {{ $errors->has('new_password') ? 'has-error' : '' }}">
        <label for="new_password" class="form-label">New Password (Minimum of 6 characters. No spaces.) <span class="required-alert">*</span></label>
        <input type="password" class="form-control" name="new_password" id="new_password" />

        @if ($errors->has('new_password'))
            <div class="help-block">
                 {{ $errors->first('new_password') }}
            </div>
       @endif

   </div>
   <div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
        <label for="confirm_password" class="form-label">Confirm New Password <span class="required-alert">*</span></label>
        <input type="password" class="form-control" name="confirm_password" id="confirm_password" />

        @if ($errors->has('confirm_password'))
            <div class="help-block">
                 {{ $errors->first('confirm_password') }}
            </div>
       @endif

  </div>
  <div class="form-group clearfix">
     <button type="submit" class="btn btn-primary">Save New Password</button>
  </div>
 </form>

控制器代码

public function updatePassword(Request $request)
{
    $this->admin = Auth::user();
    $this->id   = $this->admin->id;

    $this->validate($request, [
       'current_password'   => 'required',
       'password'           => 'required|min:6',
       'confirm_password'   => 'required|same:password',
    ]);

    if (Hash::check($request->input('current_password'), $this->admin->password)) {
        $this->admin->password = Hash::make($request->input('password'));

        $this->admin->save();

        return redirect()->back()->with('success', 'Your password has been updated.');
    } else {
        return redirect()->back()->with('warning', 'The current password you provided could not be verified');
    }
}

【讨论】:

  • 谢谢提供完整的例子。
猜你喜欢
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多