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

php中文网
发布: 2016-08-04 09:20:17
原创
1923人浏览过

<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 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 ($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);
    }</code>
登录后复制

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

法语写作助手
法语写作助手

法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。

法语写作助手 31
查看详情 法语写作助手
<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>
登录后复制

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

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号