我正在使用bshaffer/oauth2-server-php模块来验证我的REST API。一切正常,但与此同时,我在数据库中有20,000多个访问令牌。
正如我阅读的那样,该框架不会自动删除过期的令牌或通过config参数。所以我想自己做这项工作。
我知道拥有令牌的表,我已经构建了删除语句。但是我找不到合适的位置(正确的类/方法)可以与我的清理例程相连。
我认为这里的一个不错的选择是创建一个命令,在这里,Symfony的一小部分,但也可以是普通的php命令,因为您知道表名,并且每小时只执行它:
$doctrine = $this->getContainer()->get('doctrine');
$entityManager = $doctrine->getEntityManager();
$qb = $entityManager->createQueryBuilder();
$qb->select('t')
->from('OAuth2ServerBundle:AccessToken', 't')
->where('t.expires < :now')
->setParameter('now', new DateTime(), Type::DATETIME);
$accessTokens = $qb->getQuery()->getResult();
$cleanedTokens = 0;
foreach ($accessTokens as $token) {
$entityManager->remove($token);
$cleanedTokens++;
}
这只是涵盖access_token表作为一个示例。顺便说一句,我仍然无法通过此库更改令牌的到期;)
更新:要更改寿命,只需编辑参数。
oauth2.server.config:
auth_code_lifetime: 30
access_lifetime: 120
refresh_token_lifetime: 432000
我没有读取BSHAFFER OAUTH服务器的完整源。
但是想要尝试的是通过从类服务器延伸来创建自己的类。
并使用__DESTRUCT()函数在对象Customerver被php
<?php
include('src/OAuth2/Server.php'); # make sure the path is correct.
class customServer extends Server {
public __construct(($storage = array(), array $config = array(), array $grantTypes = array(), array $responseTypes = array(), TokenTypeInterface $tokenType = null, ScopeInterface $scopeUtil = null, ClientAssertionTypeInterface $clientAssertionType = null)) {
parent::_construct($storage, $config, $grantTypes, $responseTypes, $tokenType, $scopeUtil, $clientAssertionType);
}
}
public function __destruct() {
// run your cleanup SQL from here.
}
?>