【问题标题】:Custom Recaller Laravel自定义召回者 Laravel
【发布时间】:2016-07-01 05:06:42
【问题描述】:

我想将默认 cookie remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d 更改为 myprefix_web_59ba36addc2b2f9401580f014c7f58ea4e30989d

我在Illuminate\Auth\SessionGuard找到代码

/**
 * Get the name of the cookie used to store the "recaller".
 *
 * @return string
 */
 public function getRecallerName() {
   return 'remember_'.$this->name.'_'.sha1(static::class);
 }

如何创建自定义SessionGuard?有人可以帮助我吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    由于内置的​​SessionGuard 没有办法改变这一点,您需要创建自己的Guard 类来覆盖该方法,并告诉Auth 使用您的Guard 类。 my answer here 中也说明了此信息,其中说明了如何自定义 TokenGuard

    首先,首先创建一个新的Guard 类,该类扩展了基础SessionGuard 类。在您的新 Guard 类中,您将覆盖 getRecallerName() 方法以返回您想要的名称。在本例中,它创建于app/Services/Auth/MySessionGuard.php:

    namespace App\Services\Auth;
    
    use Illuminate\Auth\SessionGuard;
    
    class MySessionGuard extends SessionGuard
    {
        /**
         * Get the name of the cookie used to store the "recaller".
         *
         * @return string
         */
        public function getRecallerName()
        {
            return 'myprefix_'.$this->name.'_'.sha1(static::class);
        }
    }
    

    创建课程后,您需要通知Auth。您可以在 AuthServiceProvider 服务提供商的 boot() 方法中执行此操作:

    public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);
    
        Auth::extend('mysession', function($app, $name, array $config) {
            $provider = $this->createUserProvider($config['provider']);
    
            $guard = new \App\Services\Auth\MySessionGuard($name, $provider, $app['session.store']);        
    
            // When using the remember me functionality of the authentication services we
            // will need to be set the encryption instance of the guard, which allows
            // secure, encrypted cookie values to get generated for those cookies.
            if (method_exists($guard, 'setCookieJar')) {
                $guard->setCookieJar($app['cookie']);
            }
            if (method_exists($guard, 'setDispatcher')) {
                $guard->setDispatcher($app['events']);
            }
            if (method_exists($guard, 'setRequest')) {
                $guard->setRequest($app->refresh('request', $guard, 'setRequest'));
            }
    
            return $guard;
        });
    }
    

    最后,你需要告诉Auth 使用你的新mysession 守卫。这是在config/auth.php 配置文件中完成的。

    'guards' => [
        'web' => [
            'driver' => 'mysession',
            'provider' => 'users',
        ],
    ],
    

    【讨论】:

    • 为什么要在名称中添加sha1(static::class)
    • 我能想到的唯一好处是添加类命名空间+名称的sha1,这样您就可以知道哪个类设置了哪个会话变量,从而使不同类之间的冲突更加困难。它看起来也很神秘,但似乎并没有提供什么安全优势。
    • 在 Laravel 5.8 中试过这个并得到了Call to undefined method App\Providers\AuthServiceProvider::createUserProvider()
    • 看起来必须像这样获得提供者:$provider = resolve('auth')->createUserProvider($config['provider']);
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 2020-09-21
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2021-10-15
    • 2014-12-01
    相关资源
    最近更新 更多