简体   繁体   English

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

[英]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.

相关问题 answer 如果用户访问登录页面但已经登录,我该如何重定向? - answer How do I redirect if a user visits the login page but is already logged in? 在用户登录 php 后,我如何成功地将用户重定向到他所在的位置(他的当前页面)? - how can i successfully redirect user to where he was(his current page) after he logged in in php? Symfony2在未登录时将/ admin / *重定向到/ admin / login(或使用FOSUserBundle?) - Symfony2 redirect /admin/* to /admin/login when not logged in (or use FOSUserBundle?) 如何确保只有从特定功能重定向的用户才能访问我的网站页面? - How can I make sure that a user visits a page of my site only if he's redirected from a specific function? 如果未登录,防止用户重定向到登录页面 / Symfony3 FosUserBndl - Prevent user redirect to login page if not logged in / Symfony3 FosUserBndl 登录后将用户重定向到页面 - redirect user to a a page after he login Laravel 8 - 如何在未登录时重定向到登录页面 - Laravel 8 - How to redirect to Login page when not logged in 如果用户未登录,重定向到登录页面? - redirect to login page if user is not logged in? 如果用户未登录,则重定向到登录页面 - Redirect to login page if user is not logged in Symfony2-重新登录后重定向用户,如果由于会话过期而注销? - Symfony2 - redirect user back after re-login, if logged out because session expired?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM