简体   繁体   English

symfony2如何在登录后重定向到请求的页面

[英]How does symfony2 redirect to requested page after login

Say my route /booking/(.*) is protected by a firewall configuration in security.yml and it requires " ROLE_USER ", when user tries to access any route that is preceded by " /booking/ " the app redirects the user to login page for authentication. 假设我的路由/booking/(.*)security.yml中的防火墙配置保护,并且它需要“ ROLE_USER ”,当用户尝试访问前面带有/ booking / ”的任何路由时,app会将用户重定向到登录验证页面。

So my question is, after user provides his credentials and gets authenticated, how is Symfony 2 able to redirect the user back to the page/route the user had requested for OR where does Symfony 2 store that route does it store it in some session or some where else. 所以我的问题是,在用户提供他的凭据并获得身份验证后,Symfony 2如何能够将用户重定向回用户请求的页面/路由,或者Symfony 2存储该路由的位置是将其存储在某个会话中还是别的地方。

Can we access it and how? 我们可以访问它吗?

This is possible and you can access the referring link (that is used if use_referer is set to true ) in the session. 这是可能的,您可以在会话中访问引用链接(如果use_referer设置为true ,则使用该链接)。

For instance, if you have a success_handler service on your form_login (in your firewall configuration) that redirects users based on some criteria (commonly roles) but you wanted to redirect the user to the referrer link if it was set you could access the referrer link like so: 举例来说,如果你有一个success_handler您的服务form_login (在你的防火墙配置)重定向基于某些标准(通常角色)的用户,但你想将用户重定向到引荐链接,如果它被设置 ,你可以访问引用链接像这样:

$key = '_security.main.target_path'; #where "main" is your firewall name

//check if the referrer session key has been set 
if ($this->container->get('session')->has($key)) {
    //set the url based on the link they were trying to access before being authenticated
    $url = $this->container->get('session')->get($key);

    //remove the session key
    $this->container->get('session')->remove($key);
}
//if the referrer key was never set, redirect to a default route
else{
    $url = $this->router->generate('member_home');
}

return new RedirectResponse($url); 

Using the header to get the referrer (ie $request->headers->get('referer') ) will not work in this case because it will always return the login link. 使用标头获取引用者(即$request->headers->get('referer') )在这种情况下不起作用,因为它将始终返回登录链接。

Thanks to Roman Marintsenko & Ryan Weaver for this blog 感谢Roman Marintsenko和Ryan Weaver的博客

Carrie Kendall's solution worked, thanks! Carrie Kendall的解决方案有效,谢谢!

Here is full implementation in Symfony: 这是Symfony中的完整实现:

services.yml: services.yml:

login_handler:
    class: Project\BaseBundle\Service\loginHandler
    arguments: ['@router', '@doctrine.orm.entity_manager', '@service_container']

and in Project\\BaseBundle\\Service\\loginHandler: 在Project \\ BaseBundle \\ Service \\ loginHandler中:

namespace Project\BaseBundle\Service;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\EntityManager;


class loginHandler implements AuthenticationSuccessHandlerInterface {

    private $router;
    private $container;
    private static $key;

    public function __construct(RouterInterface $router, EntityManager $em, $container) {

        self::$key = '_security.secured_area.target_path';

        $this->router = $router;
        $this->em = $em;
        $this->session = $container->get('session');

    }

    public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {

        $user_entity = $token->getUser();

        if( !$user_entity->getChangePassword() ) {

            $route = $this->router->generate('BaseBundle_home_page');

        } else {

            $this->session->getFlashBag()->add('error', 'Your password must be changed now');

            $route = $this->router->generate('BaseBundle_account_page');

        }

        //check if the referer session key has been set
        if ($this->session->has( self::$key )) {

            //set the url based on the link they were trying to access before being authenticated
            $route = $this->session->get( self::$key );

            //remove the session key
            $this->session->remove( self::$key );
            //if the referer key was never set, redirect to a default route

        } else{

            $url = $this->generateUrl('BaseBundle_home_page');

            return new RedirectResponse($route);

        }

        return new RedirectResponse($route);

    }
}

I had this problem too. 我也有这个问题。 I used security.yml and in the registration action used: 我使用了security.yml并在使用的注册操作中:

$redirectUrl = $request->getSession()->get('_security.account.target_path');

enjoy. 请享用。

Symfony uses the HTTP Referer header to redirect a user back to the page they came from .. ie the referrer Symfony使用HTTP Referer标头将用户重定向回他们来自的页面..即引用者

You can set this using the security configuration use_referer: true in the security.yml, details here 您可以使用security.yml中的安全配置use_referer: true来设置它, 详情请参见此处

You can access the referer header from a controller using using the following : 您可以使用以下命令从控制器访问referer标头:

$referer = $request->headers->get('referer');

Note the header is miss-spelt, its referer (one r) 请注意标题拼写错误,其引用(一个r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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