gpt4 book ai didi

php - laravel 4 auth::attempt 明文密码

转载 作者:可可西里 更新时间:2023-11-01 01:03:34 26 4
gpt4 key购买 nike

你如何告诉 laravel auth::attempt 密码字段以明文形式存储而不是假设它是散列的?

在 guard.php 中

public function attempt(array $credentials = array(), $remember = false, $login = true)
{
$this->fireAttemptEvent($credentials, $remember, $login);

$user = $this->provider->retrieveByCredentials($credentials);

// If an implementation of UserInterface was returned, we'll ask the provider
// to validate the user against the given credentials, and if they are in
// fact valid we'll log the users into the application and return true.
if ($user instanceof UserInterface)
{
if ($this->provider->validateCredentials($user, $credentials))
{
if ($login) $this->login($user, $remember);

return true;
}
}

return false;
}

或者更好的是,我将只有 2 列,一列作为明文,另一列作为 password_secured。

如果我尝试后者,我如何告诉尝试密码列名称是 password_secured.

因为我尝试了这个,但得到了一个错误 Undefined index: password

    $user = array(
'user_id' => Input::get('username'),
'password_secured' => Input::get('password'),
'checklogin' => 0,
);

if (Auth::attempt($user)) {
return 'login success';
}

问题是我正在移植应用程序,而不是从头开始构建,我确实需要以明文形式存储密码,因为另一个应用程序正在使用数据库(并且它是实时的)并且被编码为读取密码明文。

最佳答案

考虑运行一个脚本来散列所有密码:绝不应该强制甚至考虑以明文形式存储(即使您继承了系统),因为这些密码是一旦您的数据库内容泄露,立即丢失。黑客攻击发生了。想象一下,如果您的客户发现您没有按照标准处理他们的数据,将会面临怎样的诉讼......

现在,假设您不想理会这个警告,那么这样做的方法虽然有点老套,但确实有效。 Guard,从来源(查找__construct)来看,被赋予一个实现UserProviderInterface的对象。

你有一堆合适的对象。选择一个你想要的,然后扩展它。我们会从 DatabaseUserProvider 中获得一些乐趣,尽管这种扩展方法很方便并且对所有这些方法都可行。

我们要扩展的方法是public function validateCredentials(UserInterface $user, array $credentials)。如下:

namespace Illuminate\Auth;
class MyUserInterface extends DatabaseUserProvider {
public function validateCredentials(UserInterface $user, array $credentials) {
$plain = $credentials['password'];
return ($plain === $user->getAuthPassword());
}
}

由于 MyUserInterface 扩展了本身提供 UserProviderInterfaceDatabaseUserProviderMyUserInterface 现在可以依赖注入(inject)到 中Guard 作为提供者。我们已经完成了一半的工作。下一步是实际告诉 Guard 加载你的东西。我不熟悉 Laravel4 加载 Guard 实现的方式,但是在配置的某个地方,您可以将 MyUserInterface 设置为选择的 Guard 接口(interface)。我不能比这更具体了。

顺便说一句,该类需要与 Auth 的其他接口(interface)实现位于同一位置。

关于php - laravel 4 auth::attempt 明文密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com