[英]Symfony2 guard inactive session lifetime
如果用戶會話在X秒內處於非活動狀態,我會嘗試使用戶會話失效。 我找到了許多解決方案,但不適用於非活動會話。
我使用Symfony2和Guard Authentification。 我已經實現了這個解決方案 ,看起來不錯。 但即使用戶處於活動狀態,會話也會過期。 我可能會錯過一些東西 使用Guard可以影響會話時間有什么特別之處嗎?
我的身份驗證員 :
namespace AppBundle\Security;
class TokenAuthenticator extends AbstractGuardAuthenticator
{
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
private $router;
/*
* Url d'accès au WebService d'authentification
*/
private $urlWs;
/**
* Constructeur
* @param RouterInterface $router
* @param string $urlWs : Url d'accès au WebService d'authentification
*/
public function __construct(RouterInterface $router, $urlWs) {
$this->router = $router;
$this->urlWs = $urlWs;
}
/**
* Called on every request. Return whatever credentials you want,
* or null to stop authentication.
*/
public function getCredentials(Request $request)
{
if ($request->getPathInfo() != "/login_check"){
return;
}
// What you return here will be passed to getUser() as $credentials
return [
'login' => $request->request->get('username'),
'password' => $request->request->get('password'),
'request' => $request,
];
}
/**
*
* @param type $credentials
* @param UserProviderInterface $userProvider
* @return User
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
$login = $credentials['login'];
$user = new User();
$user->setLogin($login);
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
$username = $credentials['login'];
$password = $credentials['password'];
try {
/**********************************
Call my WebService to control the login password
If it's ok, I save the returned user in session
*****************************************/
return true;
} else {
throw new CustomUserMessageAuthenticationException($ws_response->messages[0]);
}
}catch(\Exception $e){
throw $e;
}
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
$session = $request->getSession();
$url = "/";
return new RedirectResponse($url);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
$url = $this->router->generate('loginSf');
return new RedirectResponse($url);
}
/**
* Called when authentication is needed, but it's not sent
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$url = $this->router->generate('loginSf');
return new RedirectResponse($url);
}
public function supportsRememberMe()
{
return false;
}
}
我的會話處理程序
namespace AppBundle\Handler;
class SessionIdleHandler
{
protected $session;
protected $securityContext;
protected $router;
protected $maxIdleTime;
public function __construct(SessionInterface $session, SecurityContextInterface $securityContext, RouterInterface $router, $maxIdleTime = 0)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->router = $router;
$this->maxIdleTime = $maxIdleTime;
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
$event->setResponse(new RedirectResponse($this->router->generate('loginSf')));
}
}
}
}
service.yml :
services:
my.handler.session_idle:
class: AppBundle\Handler\SessionIdleHandler
arguments: ["@session", "@security.context", "@router", %sessionLifeTime%]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
config.yml :
framework:
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
cookie_lifetime: %sessionLifeTime%
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
在parameters.yml我這樣: sessionLifeTime: 0
我也找到了這個選項 ,但它是解決我的問題的方法嗎?
你能幫助我嗎 ? 謝謝。
最簡單的方法是通過合理頻繁運行的垃圾收集來實現這一點。
你有一個關於空閑時間會話的symfony doc的段落。
謝謝你的幫助,
我不明白我做了什么。 我測試您的解決方案(所以我評論我的會話處理器)把gc_maxlifetime上config.yml,但我的問題仍然存在。
所以我取消了對會話處理程序的注釋,最后我回到了之前發布的代碼......現在它可以工作了......
對不起,我無法解釋。 我已多次清除緩存,所以我認為這不是原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.