[英]How can I redirect logged in user in Symfony2 when he visits the login page?
I need to redirect a user when he visits the login page being already logged. 当用户访问已记录的登录页面时,我需要重定向用户。 I found some question, however the propose solution doesn't work for me.
我发现了一个问题,但是提议的解决方案对我不起作用。 Here is the original question: Redirect already logged in user Symfony2
这是原始问题: 重定向已登录用户Symfony2
It doesn't work for me producing AuthenticationCredentialsNotFoundException
. 这对我产生
AuthenticationCredentialsNotFoundException
无效。
Here is my firewall configuration: 这是我的防火墙配置:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/secured/login$
security: false
password_reset:
pattern: ^/secured/login\-password/
security: false
secured_area:
pattern: ^/secured/
form_login:
check_path: /secured/login_check
login_path: /secured/login
default_target_path: /secured/app
#provider: chain_provider
provider: user_db
success_handler: acme.security.authentication.success_handler
failure_handler: acme.security.authentication.failure_handler
logout:
path: /secured/logout
target: /secured/login
remember_me:
key: "%secret%"
lifetime: 31536000 # 365 days in seconds
path: /
domain: ~ # Defaults to the current domain from $_SERVER
The proposed solution in the question I mentioned leads to the following exception: 我提到的问题中的拟议解决方案导致以下异常:
AuthenticationCredentialsNotFoundException: The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.
What am I doing wrong and how to solve this problem? 我在做什么错以及如何解决这个问题?
Here is my solution. 这是我的解决方案。 As I don't have access to the token through API, I decided to take it from Session and pass (if found) and pass unserialized value to the access decision manager (that is normally used internally in the security context).
由于我无法通过API访问令牌,因此我决定从Session中获取令牌并传递(如果找到),并将未序列化的值传递给访问决策管理器(通常在安全性上下文内部使用)。 Maybe there can be a better solution, but at least this will work till the moment when the better one will become evident.
也许可以有一个更好的解决方案,但至少要等到更好的解决方案变得明显的那一刻,它才能起作用。
<?php
namespace Acme\Bundle\Bundle\EventListener;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LoggedInListener
{
const APP_URL = 'acme_secured_app';
const LOGIN_URL = 'acme_secured_login';
private $router;
private $decisionManager;
/**
* @param Router $router
* @param AccessDecisionManagerInterface $decisionManager
*/
public function __construct(Router $router, AccessDecisionManagerInterface $decisionManager)
{
$this->router = $router;
$this->decisionManager = $decisionManager;
}
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$firewallName = 'secured_area';
$tokenStr = $event->getRequest()->getSession()->get('_security_'.$firewallName);
if (!$tokenStr) {
return;
}
$token = unserialize($tokenStr);
if (!$token) {
return;
}
$authenticated = $this->decisionManager->decide($token, array('IS_AUTHENTICATED_FULLY'));
if ($authenticated) {
// authenticated (NON anonymous)
$routeName = $event->getRequest()->attributes->get('_route');
if ($routeName == self::LOGIN_URL) {
$url = $this->router->generate(self::APP_URL);
$event->setResponse(new RedirectResponse($url));
}
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.