php - 在 PHP 7.2 上使用 Symfony 会话的 `migrate` 方法会引发错误
问题描述
我们在 Symfony 应用程序上使用 Guard 身份验证器通过外部应用程序进行身份验证。在getCredentials
我们AbstractGuardAuthenticator
实现的方法中,我们使用migrate
Request 的 session 上的方法来刷新 session。
/**
* Called on every request and refreshes the user's session.
* Attempts to get credentials (i.e. a token) from a request:
* If a token is present, attempts to log a user in. Continues to the getUser method.
* If there is no token a user is logged in, the request continues.
* If there is no token and there is no logged in user, sends the User to authenticate.
*
* @param Request $request The current request
* @return array|null If there is a token present, it is returned in an array; otherwise null is returned
*/
public function getCredentials(Request $request)
{
// Update the session lifetime
// The migrate method regenerates the session to a new session id, but preserves
// all attributes. The first parameter is set to true so that the old session
// is destroyed, and the second parameter resets the lifetime to 30 minutes.
$request->getSession()->migrate(true, 30 * 60);
// Check query params for token
$token = $request->query->get('token');
// When no token is provided:
// If user is logged in, skip authentication steps
// If no user is logged in, go to start method to authenticate
if($token == null)
return null;
// Token provided, continue to getUser
return ['token' => $token];
}
在 PHP 7.1 上运行时,这工作正常。根据文档,该migrate
方法返回 true,表示迁移成功。在 PHP 7.2 上运行时,我收到此错误:
Symfony\Component\Debug\Exception\ContextErrorException:
Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time
at vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage.php:201
at Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->regenerate(true, 1800)
(vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Session.php:185)
at Symfony\Component\HttpFoundation\Session\Session->migrate(true, 1800)
(vendor\OrgOne\authentication-bundle\OrgOne\AuthenticationBundle\Security\TokenAuthenticator.php:55)
at OrgOne\AuthenticationBundle\Security\TokenAuthenticator->getCredentials(object(Request))
(vendor\symfony\symfony\src\Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener.php:118)
我们应该如何使用migrate
PHP 7.2 中的方法或类似的东西?
解决方案
我可以用 PHP 7.2.7 和 symfony 4.2 重现这种行为。
不带参数调用该migrate
函数不会产生异常
$request->getSession()->migrate()
framework.yml
然后将使用配置中指定的生命周期。所以你可以设置 cookie_lifetime 为 1800 (= 30 * 60)
framework:
...
session:
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_lifetime: 1800
推荐阅读
- regex - 当整行不重复时,使用 REGEX 删除重复项
- amazon-web-services - 启动 Amazon EC2 Spot 实例以编程方式运行 docker 映像
- java - Spring数据将所有关联资源反序列化为_embedded
- python - 如何在 python 中将 .csv 文件转换为 .bag?
- python-3.x - python中带有标志的情节烛台
- javascript - 基于 url 的页面更新的 React useLocation 钩子的替代方案?
- node.js - LDAP 获取全名和电子邮件 nodejs
- python - f2py 编译的 Fortran 模块中的垃圾收集
- python - Matplotlib:倒置图的组合
- php - 如何使用 PHP 和 SSH 连接到 Amazon EC2 服务器