【问题标题】:Extending Laravel 5.2 SessionGuard扩展 Laravel 5.2 SessionGuard
【发布时间】:2016-07-05 08:46:59
【问题描述】:

我想扩展 Laravel 库存身份验证以使用 OAuth 服务器进行用户检索和身份验证,同时利用现有功能。我已经设法扩展EloquentUserProvider 以部分覆盖/扩展Illuminate\Contracts\Auth\UserProvider 合约的实现。当前的实现如下所示:

class EloquentOauthServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return HcOAuthProvider;
     */
    public function boot()
    {
        Auth::provider('oauth',function($app){
            $model = $app['config']['auth.providers.oauth.model'];
            $repository = new OauthUserRepository();
            return new EloquentOauthUserProvider($app['hash'], $model, $repository);
        });
    }

}

auth.php 配置中,我更改了这样的描述符:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'oauth',
    ],
]

'providers' => [
    'oauth' => [
        'driver' => 'oauth',
        'model' => App\Models\User::class,
    ],
]

到目前为止,这是有效的,我可以覆盖方法来添加我的逻辑。但我意识到我还需要覆盖SessionGuard 类的一些方法(具体来说是loginlogout),因为我想使用Laravel 会话实现来保存和检索OAuth 特定令牌。有一些建议,但它们要么不起作用(也许它们在 Laravel 5.2 之前起作用),要么需要覆盖 Authmanager,这看起来有点矫枉过正。

所以我的问题是:我必须在 Laravel 5.2 中做什么来覆盖 SessionGuard?

【问题讨论】:

    标签: php laravel authentication laravel-5.2


    【解决方案1】:

    最近我也遇到了同样的问题,所以可能解决方案是这样的。

    <?php
    namespace App\CoreExtensions;
    use Illuminate\Auth\SessionGuard;
    use Illuminate\Contracts\Auth\Authenticatable;
    class SessionGuardExtended extends SessionGuard
    {
        /**
         * Log a user into the application.
         *
         * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
         * @param  bool  $remember
         * @return void
         */
        public function login(Authenticatable $user, $remember = false)
        {
            $this->updateSession($user->getAuthIdentifier());
            if ($remember) {
                $this->refreshRememberToken($user);
                $this->queueRecallerCookie($user);
            }
            $this->fireLoginEvent($user, $remember);
            $this->setUser($user);
        }
    }
    

    在 AppServiceProvider.php 中

    public function boot()
    {
        Auth::extend(
            'sessionExtended',
            function ($app) {
                $provider = new EloquentUserProvider($app['hash'], config('auth.providers.users.model'));
                return new SessionGuardExtended('sessionExtended', $provider, app()->make('session.store'), request());
            }
        );
    }
    

    在 Config/auth.php 中应该重命名驱动

    'web' => [
        'driver' => 'sessionExtended',
        'provider' => 'users',
    ],
    

    【讨论】:

    • 有用的答案。对于那些看这个的人,我只想添加一个对文档身份验证Adding Custom Guards 的引用。上面的答案遵循那里给出的相同模式,只是在 auth.config 中它是“替换内置防护”而不是“添加新防护”
    【解决方案2】:

    扩展保护:

    use Illuminate\Auth\SessionGuard;
    
    class MySessionGuard extends SessionGuard {
        // ...
    }
    

    更新配置

    'web' => [
        'driver' => 'my-session',
        'provider' => 'users',
    ],
    

    在 AuthServiceProvider.php 中绑定新的守卫:

    public function boot()
        {
            Auth::extend(
                'fmc-sso-session',
                function ($app, $name, array $config) {
                    /* Copy functionality form Auth::createSessionDriver */
                    $guard = new MySessionGuard($name, Auth::createUserProvider($config['provider'] ?? null), $this->app['session.store']);
                    if (method_exists($guard, 'setCookieJar')) {
                        $guard->setCookieJar($this->app['cookie']);
                    }
                    if (method_exists($guard, 'setDispatcher')) {
                        $guard->setDispatcher($this->app['events']);
                    }
                    if (method_exists($guard, 'setRequest')) {
                        $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
                    }
    
                    return $guard;
                }
            );
    
           // ...
        }
    

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 2016-06-17
      • 1970-01-01
      • 2018-06-16
      • 2017-03-11
      • 2018-02-22
      • 2016-07-03
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多