繁体   English   中英

当Symfony2中的登录用户访问登录页面时,该如何重定向他?

[英]How can I redirect logged in user in Symfony2 when he visits the login page?

当用户访问已记录的登录页面时,我需要重定向用户。 我发现了一个问题,但是提议的解决方案对我不起作用。 这是原始问题: 重定向已登录用户Symfony2

这对我产生AuthenticationCredentialsNotFoundException无效。

这是我的防火墙配置:

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

我提到的问题中的拟议解决方案导致以下异常:

AuthenticationCredentialsNotFoundException: The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.

我在做什么错以及如何解决这个问题?

这是我的解决方案。 由于我无法通过API访问令牌,因此我决定从Session中获取令牌并传递(如果找到),并将未序列化的值传递给访问决策管理器(通常在安全性上下文内部使用)。 也许可以有一个更好的解决方案,但至少要等到更好的解决方案变得明显的那一刻,它才能起作用。

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM