【问题标题】:Laravel: what is storage key in auth.php?Laravel:auth.php 中的存储密钥是什么?
【发布时间】:2020-05-08 21:04:41
【问题描述】:

来自API Authentication,它说

如果您选择使用不同的列名,请务必在 config/auth.php 配置文件中更新您的 API 的 storage_key 配置选项。

如果 api_token 名称是 Laravel API 身份验证的默认名称,我会使用 token 列。它说 config storage_key 使用不同的名称。我找不到这样的东西,谷歌搜索没有说什么。什么是storage_key,如何使用它来将token 设置为API 身份验证列名而不是api_token

提前致谢

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    这是Illuminate/Auth/AuthManager里面的方法

    /**
     * Create a token based authentication guard.
     *
     * @param  string  $name
     * @param  array  $config
     * @return \Illuminate\Auth\TokenGuard
     */
    public function createTokenDriver($name, $config)
    {
        // The token guard implements a basic API token based guard implementation
        // that takes an API token field from the request and matches it to the
        // user in the database or another persistence layer where users are.
        $guard = new TokenGuard(
            $this->createUserProvider($config['provider'] ?? null),
            $this->app['request'],
            $config['input_key'] ?? 'api_token',
            $config['storage_key'] ?? 'api_token'
        );
    
        $this->app->refresh('request', $guard, 'setRequest');
    
        return $guard;
    }
    

    它与provider处于同一级别,从课堂上也可以看出它与driver处于同一级别。您需要将storage_keyinput_key 放在您将用于身份验证的选定guards(例如api)之一中。默认情况下,它们都设置为api_token

    'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
            'api' => [
                'driver' => 'token',
                'provider' => 'users',
            ],
        ],
    

    【讨论】:

    • 谢谢,因此我可以有一些像这样的东西 'api' => [ 'driver' => 'token', 'provider' => 'users', 'storage_key' => 'token' ],
    • 没错@MamaD,你也可以追加input_key覆盖默认值api_token
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2010-09-18
    • 2019-09-15
    • 2018-11-29
    相关资源
    最近更新 更多