Php 如何使用Yii2进行正确的跨子域身份验证?

Php 如何使用Yii2进行正确的跨子域身份验证?,php,authentication,yii2,session-cookies,yii2-basic-app,Php,Authentication,Yii2,Session Cookies,Yii2 Basic App,面对这个问题。 我为主域创建了两个服务器别名(如子域ru.testproject.local和en.testproject.local)。还进行了跨子域身份验证,在Yii2(基本应用程序)的配置文件中使用此代码: 根据这一点,身份验证也适用于主域和子域。但是我不能在主域上注销(虽然在子域上我可以这样做)。我已尝试删除enableAutoLogin属性。当然,它能像我需要的那样工作。但这不是一个好的解决方案,因为当用户关闭浏览器并再次打开它时,我们还需要再次登录。我能做些什么来修理这个 提前谢谢

面对这个问题。 我为主域创建了两个服务器别名(如子域ru.testproject.local和en.testproject.local)。还进行了跨子域身份验证,在Yii2(基本应用程序)的配置文件中使用此代码:

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


提前谢谢

你能试试这个吗

    '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,
        ],
    ],
登录后,通过浏览器控制台确认主网站和子域网站的_identitycookie域和路径相同


已更新

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

会话配置的默认类是
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];
/**
 * @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];
至于
user
config数组,它主要针对当前访客状态使用
yii\web\user
,无论是访客还是登录用户。下面是代码中的注释

/**
 * @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];
/**
 * @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设置不使用“path”参数(尽管会话使用这个参数)。结果,一切正常。但为什么会这样呢?提前感谢当我在cookieParams下添加cookie“name”时,我发现这是有效的。所有子域都必须相同。