• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何重写Laravel 的attempt方法呢?因为加密方法是自定义的。

php 搞代码 3年前 (2022-01-23) 20次浏览 已收录 0个评论
文章目录[隐藏]
<code>Auth::attempt(array('username' => $username, 'password' => $password),false)</code>

这个东西里头password是想用自己定义的方法加密

回复内容:

<code>Auth::attempt(array('username' => $username, 'password' => $password),false)</code>

这个东西里头password是想用自己定义的方法加密

文档确实没有写,但是我们可以看看源码

Auth方法的实现都在 Illuminate\Auth\Guard里面

<code>    /**     * Attempt to authenticate a user using the given credentials.     *     * @param  array  $credentials     * @param  bool   $remember     * @param  bool   $login     * @return bool     */    public function attempt(array $credentials = [], $remember = false, $login = true)    {        $this->fireAttemptEvent($credentials, $remember, $login);        $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);                            // 看这里        // If an implementation of UserInterface was returned, we'll ask the provider        // to validat<b>6本文来源gao@dai!ma.com搞$代^码!网7</b><pre>搞gaodaima代码

e 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 ($this->hasValidCredentials($user, $credentials)) { if ($login) { $this->login($user, $remember); } return true; } return false; } /** * Determine if the user matches the credentials. * * @param mixed $user * @param array $credentials * @return bool */ protected function hasValidCredentials($user, $credentials) { // 执行认证驱动器的validCredentials方法 return ! is_null($user) && $this->provider->validateCredentials($user, $credentials); }

默认是使用eloquent作为认证驱动器,所以看看Illuminate\Auth\EloquentUserProvider里面的实现

<code>    public function validateCredentials(UserContract $user, array $credentials)    {        $plain = $credentials['password'];        return $this->hasher->check($plain, $user->getAuthPassword());    }</code>

所以如果要改验证的逻辑,可以继承原有的驱动器,然后重写validateCredentials里面的逻辑

<code>class TestUserProvider extend EloquentUserProvider{    public function validateCredentials(UserContract $user, array $credentials)    {        $plain = $credentials['password'];        return md5($plain) == $user->getAuthPassword();    }}</code>

最后设置驱动器,建议加载AppServiceProvider的boot()里面

<code>Auth::setProvider(new TestUserProvider());</code>

文档里有写!不要偷懒不看文档,你最近提的问题都是文档里写的。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何重写Laravel 的attempt方法呢?因为加密方法是自定义的。

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址