使用目标路径登录Symfony后绑定重定向,但始终为null

时间:2018-09-23 09:49:28

标签: symfony authentication symfony-3.4

我已经看到$this->getTargetPath($request->getSession(), $providerKey)可用于获取目标路径,但它始终是null

有人可以解释这是什么意思,为什么它对我来说总是null

use Symfony\Component\Security\Http\Util\TargetPathTrait;


class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{

    use TargetPathTrait;

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
        {
            $targetPath = $this->getTargetPath($request->getSession(), $providerKey);

            if (!$targetPath) {
                $targetPath = $this->container->get('router')
                    ->generate('poll_index');
            }

            return new RedirectResponse($targetPath);
        }
}

1 个答案:

答案 0 :(得分:1)

从Symfony设置target path的唯一时间是用户通过身份验证入口点开始身份验证流程。这是由ExceptionListener完成的。如果您使用的是FormAuthenticationEntryPoint,则当您尝试访问受限页面时(通常)将您重定向到登录页面时,会发生这种情况。此时已设置target path

通常没有理由自己进行设置,但是您可以这样使用TargetPathTrait的saveTargetPath进行设置:

$this->saveTargetPath($request->getSession(), $providerKey, $request->getUri());