本文介绍了在security.yml中获取当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的要求中,用户收到一封带有URL的电子邮件,一旦单击该用户,便会通过身份验证过程将其导航到该URL.

In my requirement, a user receives an email with a url, once he clicks the user will be navigated to the url via an authentication process.

因此要将用户重定向到点击的网址,我正在使用此处提到的方法(重定向时传递参数),我打算将重定向url作为参数传递给

So to redirect the user to the clicked url I am using the method mentioned here ( Pass parameters when redirect ) where I intend to pass the redirect url as parameter like

login_path: %accounts_host%/signin?redirect=%need_current_url_here%

并捕获为$url=$_GET['redirect'];并相应地提供重定向.

within the security.yml and capture as such $url=$_GET['redirect']; and provide the redirection accordingly.

我的查询是如何从security.yml内部访问当前URL,以便可以将其附加到login_path.

My query is how can I access the current url from within the security.yml so that I can attach it to the login_path.

我对此很陌生,非常感谢任何示例/文档. :)

I am quite new to this and any example/ document is very much appreciated. :)

PS

身份验证是在另一个symonfy2应用程序中完成的,此时,我不能使用referer命令,因为它将为null.这就是为什么我尝试将重定向网址作为参数传递的原因. :)

The authentication is done within another symonfy2 application at which point, I cant use referer command as it will be null. That is why I am trying to pass thee redirect url as a parameter. :)

推荐答案

我建议使用入口点和成功处理程序.

I would suggest to use an entry point and a success handler.

security.yml:

security.yml:

firewalls:            # Required
    # Examples:
    somename:
        entry_point: some.service.id
        ...
        form_login:
            ...
            success_handler: some.service.id

SuccessHandler():

SuccessHandler (source):

<?php
namespace StatSidekick\UserBundle\Handler;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {

    public function __construct( HttpUtils $httpUtils, array $options ) {
        parent::__construct( $httpUtils, $options );
    }

    public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
        // Create if necessary a redirect response otherwise use the parent one with
        // $response = parent::onAuthenticationSuccess( $request, $token );

        return $response;
    }
}

入口点():

这篇关于在security.yml中获取当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:42