场景:今天部署负载均衡的时候YII2后台用户登录总是登录成功之后跳转后台首页就无法进入说是用户未登录,调试也是有session,但是就是无法获取到【\Yii::$app->user->identity】。
解决办法:最后经过调试发现config/web.php的[components]配置是有顺序的,请务必保证session组件在user组建的前面,我姑且放到第一位了。
'components' => [
'session' => [
'class' => 'yii\redis\Session',
'keyPrefix'=>'admin_session:',
'redis' => 'redis',
'cookieParams' => [
// 'domain' => 'admin233.nftwc.cn',
'httponly' => false,
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'admin_kiYGawmDi_2DoFAHH1OkaQnE6Db2F',
"enableCsrfValidation" => false
],
'cache' => [
'class' => 'yii\caching\FileCache'
],
'memcache' => [
'class' => 'yii\caching\MemCache',
'servers' => [
[
'host' => $_SERVER['MEM_HOST'],
'port' => $_SERVER['MEM_PORT'],
'weight' => $_SERVER['MEM_WEIGHT']
]
],
'useMemcached' => true // 是否开启[memcached]
],
'redis' => [ // 其他缓存,比如:文章数据统计等非数据库数据杂乱的数据
'class' => 'yii\redis\Connection',
'hostname' => $_SERVER['REDIS_HOST'],
'port' => $_SERVER['REDIS_PORT'],
'database' => 0,
'password' => $_SERVER['REDIS_PWD']
],
'redisDb' => [ // 数据库Redis缓存专用
'class' => 'yii\redis\Connection',
'hostname' => $_SERVER['REDIS_HOST'],
'port' => $_SERVER['REDIS_PORT'],
'database' => 1,
'password' => $_SERVER['REDIS_PWD']
],
'queue' => [
//Redis 队列方案
'class' => \yii\queue\redis\Queue::className(),
// 连接组件或它的配置
'redis' => 'redis',
// Queue channel key
'channel' => 'queue',
'as log'=> \yii\queue\LogBehavior::className(),
'serializer' => \yii\queue\serializers\JsonSerializer::className(),
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\modules\backend\model\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity', 'httpOnly' => false],
'idParam' => '__user',
'loginUrl' => '/login.html?showHint=1',
],
]