请问APPSERVER下面的API的登录态过期时间在哪里配置?

问题咨询 · zks888 · 于 5年前 发布 · 1993 次阅读

走了RESTful以后,enableSession设置成false以后,session的过期时间就不能用来控制登录态的过期时间了,那现在登录态的过期时间要怎么配置呀?

目前我是这么配置的,登录态一个小时就过期了,求大神指导一下,多谢!

$components = [

'user' => [
    'class'             => 'fecshop\yii\web\User',
    'identityClass'     => 'common\local\local_models\mysqldb\Customer',
    'enableSession'     => false,
    'enableAutoLogin'   => true,
    'loginUrl'          => null,
],

];

共收到 4 条回复
Fecmall#15年前 0 个赞

按照代码排查一下就知道了,你想要的是appserver用户的登陆过期时间

1.所有的登陆后的controller,都继承这个类 https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/AppserverTokenController.php#L71

在71行代码可以看到,登陆验证是:use fecshop\yii\filters\auth\QueryParamAuth;

打开这个文件:https://github.com/fecshop/yii2_fecshop/blob/master/yii/filters/auth/QueryParamAuth.php

可以看到:

 $identity = Yii::$service->customer->loginByAccessToken(get_class($this));

也就是登陆是这个部分

2.继续找

打开:https://github.com/fecshop/yii2_fecshop/blob/master/services/Customer.php#L675

可以看到代码

/** @var \fecshop\models\mysqldb\Customer|null $identity */
        $identity = Yii::$app->user->loginByAccessToken($accessToken, $type);
        if ($identity !== null) {
            $access_token_created_at = $identity->access_token_created_at;
            $timeout = Yii::$service->session->timeout;
            // 如果时间没有过期,则返回 identity
            if ($access_token_created_at + $timeout > time()) {
                // 如果时间没有过期,但是快要过期了,在过$updateTimeLimit段时间就要过期,那么更新access_token_created_at。
                $updateTimeLimit = Yii::$service->session->updateTimeLimit;
                if ($access_token_created_at + $timeout <= (time() + $updateTimeLimit)) {
                    $identity->access_token_created_at = time();
                    $identity->save();
                }
                return $identity;
            } else {
                $this->logoutByAccessToken();
                return null;
            }
        }

https://github.com/fecshop/yii2_fecshop/blob/master/yii/filters/auth/QueryParamAuth.php#L27

过期时间就是$updateTimeLimit = Yii::$service->session->updateTimeLimit;

就是session services 的 updateTimeLimit配置的,也就是这里配置的: https://github.com/fecshop/yii2_fecshop/blob/master/config/services/Session.php#L22

你在本地@appserver/config/fecshop_local_services/ 下面的Session.php(没有这个文件自行创建),重写配置就可以了

如果你不明白如何重写,那么手把手的和你说一下

1.新建文件@appserver/config/fecshop_local_services/Session.php ,将 https://github.com/fecshop/yii2_fecshop/blob/master/config/services/Session.php#L22 里面的内容复制进去,然后把除了 'updateTimeLimit' => 600,其他的子项去掉,然后设置这个值就可以了。

zks888#25年前 0 个赞

多谢大神指导,已看懂,已设置好。

Fecmall#35年前 0 个赞

@zks888 #2楼

上面说错了,超时时间是 $timeout = Yii::$service->session->timeout;

if ($access_token_created_at + $timeout > time()) {
                // 如果时间没有过期,但是快要过期了,在过$updateTimeLimit段时间就要过期,那么更新access_token_created_at。
                $updateTimeLimit = Yii::$service->session->updateTimeLimit;
                if ($access_token_created_at + $timeout <= (time() + $updateTimeLimit)) {
                    $identity->access_token_created_at = time();
                    $identity->save();
                }
                return $identity;
            } else {
                $this->logoutByAccessToken();
                return null;
            }

1.登陆状态过期时间为:$timeout = Yii::$service->session->timeout;

2.到期后,就需要重新登陆,为了可以让持续用户访问用户不过期,就需要每次访问都更新$identity->access_token_created_at,但是这对数据库的update会有压力,因此,当用户访问到达一个阀值,在更新 access_token_created_at, 这个值就是 updateTimeLimit, 如果时间没有过期,但是快要过期了,在过$updateTimeLimit段时间就要过期,那么更新access_token_created_at

因此如果updateTimeLimit设置为0,那么到期后,就需要用户重新登陆,如果设置了这个值,用户会在token快要过去的时候进行更新

因此过期时间是:$timeout = Yii::$service->session->timeout; , 您也需要设置updateTimeLimit, updateTimeLimit 需要比 $timeout 小,譬如:

过期时间设置6 60 60 (6小时),然后设置 updateTimeLimit为 30 * 60 (半小时),那么用户登陆后,过期时间为6小时,当用户在5个小时30分钟的时候访问,那么就会达到阀值,就会更新$identity->access_token_created_at = time();,token的过期时间又会重新变成6小时,这样用户持续访问可以一直在线,又不会对数据库造成压力

当然,最好的方式还是jwt,但是用这种方式,yii2 user的很多东西就不能用了,因此,目前用的还是yii2 user这一套实现的

如果并发大,可以考虑重写一下,将其放到redis等高性能的数据库里面

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics