【问题标题】:How to make properly cross-subdomain authentication with Yii2?如何使用 Yii2 进行正确的跨子域身份验证?
【发布时间】:2016-04-07 12:02:58
【问题描述】:

面临问题。 我为主域创建了 2 个服务器别名(如子域 ru.testproject.local 和 en.testproject 。当地的)。还做了一个跨子域认证,在Yii2(基础应用)的配置文件中使用了这段代码:

'user' => [
  //'class' => 'yii\web\User',
  'identityClass' => 'app\models\Users',
  'enableAutoLogin' => true,
  'identityCookie' => [
    'name' => '_identity',
    'httpOnly' => true,
    'path' => '/',
    'domain' => '.testproject.local',
  ],
],
'session' => [
  //'savePath' => '\app\session',
  'cookieParams' => [
    'domain' => '.testproject.local',
    //'httpOnly' => true,
    //'path' => '/',
  ],
],

根据这一点,身份验证也适用于主域和子域。但是我不能在主域上注销(尽管在子域上我可以这样做)。我试图删除 enableAutoLogin 属性。当然,它就像我需要的那样工作。但这不是一个好的解决方案,因为当用户关闭浏览器并再次打开时,我们还需要再次登录。我能做些什么来解决这个问题?

提前致谢!

【问题讨论】:

    标签: php authentication yii2 session-cookies yii2-basic-app


    【解决方案1】:

    你可以试试这个吗?

        'user' => [
            'identityClass' => 'app\models\Users',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_identity',
                'httpOnly' => true,
                'domain' => '.testproject.local',
            ],
        ],
        'session' => [
            'cookieParams' => [
                'domain' => '.testproject.local',
                'path' => '/',
                'httpOnly' => true,
                'secure' => false,
            ],
        ],
    

    登录后,通过浏览器控制台确认主域和子域网站的_identity cookie域和路径相同。


    更新

    跟踪当前访问者会话的实际 cookie 实际上是 session 配置数组。

    会话配置的默认类是yii\web\Session。 这是代码中的注释。

    /**
     * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function
     * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly'
     * @see http://www.php.net/manual/en/function.session-set-cookie-params.php
     */
    private $_cookieParams = ['httponly' => true];
    

    user 配置数组,它使用yii\web\User 主要用于当前访问者状态,无论是访客还是登录用户。下面是代码中的注释。

    /**
     * @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is true.
     * @see Cookie
     */
    public $identityCookie = ['name' => '_identity', 'httpOnly' => true];
    

    【讨论】:

    • 非常感谢!它现在真的有效。但是你能解释一下,我的代码有什么问题吗?正如我所见,您不使用“路径”参数进行 cookie 设置(尽管使用此参数进行会话)。结果,一切正常。但为什么会发生呢?提前致谢
    • 我发现当我在 cookieParams 下添加 cookie 'name' 时这有效。所有子域必须相同。
    猜你喜欢
    • 2014-10-25
    • 2019-02-25
    • 2015-08-21
    • 2016-01-05
    • 1970-01-01
    • 2020-02-28
    • 2021-01-13
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多