laravel护照自定义密码栏
laravel passport custom password column
如何使用 Laravel 的 Passport 包来验证不同的密码列。
如果我想从不同的 'username' 列进行身份验证,可以使用以下代码完成:
public function findForPassport($username) {
return $this->where('id', $username)->first();
}
它将以Id为列。如果我想使用不同的 'password' 列怎么办? table 中具有不同名称的列,例如 'uid_token'.
Passport/Bridge 要求有一个名为 validateForPassportPasswordGrant($password)
的方法,您可以在 user model
中覆盖它,如果您不覆盖它,它将寻找 password
列在您的用户 table
中。我不完全确定为什么他们没有将其配置为使用 Authenticatable
方法 getAuthPassword...
将此 validateForPassportPasswordGrant
方法添加到用户模型为我完成了工作("PasswMd" - 自定义列名称):
public function validateForPassportPasswordGrant($password)
{
return Hash::check($password, $this->PasswMd);
}
虽然上述解决方案很棒,但还有另一种方法可以实现,它在 Laravel 8 对我有用。
对于未来的读者,我在此处提供了代码,他们需要像这样添加到他们的模型和 return 自定义密码列。
public function getAuthPassword()
{
return $this->PasswMd;
}
如何使用 Laravel 的 Passport 包来验证不同的密码列。
如果我想从不同的 'username' 列进行身份验证,可以使用以下代码完成:
public function findForPassport($username) {
return $this->where('id', $username)->first();
}
它将以Id为列。如果我想使用不同的 'password' 列怎么办? table 中具有不同名称的列,例如 'uid_token'.
Passport/Bridge 要求有一个名为 validateForPassportPasswordGrant($password)
的方法,您可以在 user model
中覆盖它,如果您不覆盖它,它将寻找 password
列在您的用户 table
中。我不完全确定为什么他们没有将其配置为使用 Authenticatable
方法 getAuthPassword...
将此 validateForPassportPasswordGrant
方法添加到用户模型为我完成了工作("PasswMd" - 自定义列名称):
public function validateForPassportPasswordGrant($password)
{
return Hash::check($password, $this->PasswMd);
}
虽然上述解决方案很棒,但还有另一种方法可以实现,它在 Laravel 8 对我有用。
对于未来的读者,我在此处提供了代码,他们需要像这样添加到他们的模型和 return 自定义密码列。
public function getAuthPassword()
{
return $this->PasswMd;
}