Get current url within security.yml

馋奶兔 提交于 2019-12-25 04:07:11

问题


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.

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%

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

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

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. :)


回答1:


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

security.yml:

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

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;
    }
}

Entry point (source):

When the user is not authenticated at all (i.e. when the token storage has no token yet), the firewall's entry point will be called to "start" the authentication process. An entry point should implement AuthenticationEntryPointInterface, which has only one method: start() ...



来源:https://stackoverflow.com/questions/27898039/get-current-url-within-security-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!