重新登录后修改symfony重定向路径 [英] Modify symfony redirect path after re-login

查看:54
本文介绍了重新登录后修改symfony重定向路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多类似的问题,但没有找到答案.那么,当会话过期时,用户将被重定向到登录页面.然后,用户输入登录名/密码,symfony 将他重定向到上一页,例如 /page.我想将用户重定向到 #/page,所以我需要将 /# 字符串添加到引用路径.我怎样才能做到这一点?我正在使用 FOSUserBundle,但看起来这就是 symfony 所做的.有什么想法吗?

I have viewed a lot of similar questions but haven't found the answer. Well, when the session expired, user will be redirected to the login page. Then, user insert login/password and symfony redirects him to the previous page, like /page. I want redirect user to #/page, so I need add /# string to referer path. How can I do that? I'm using FOSUserBundle, but looks like that is what the symfony do. Any ideas?

推荐答案

我已经找到了解决方案.我们需要扩展 Symfony 安全组件 DefaultAuthenticationSuccessHandler,更具体地说 - determineTargetUrl 方法.

I have figure out the solution. We need to extend the Symfony security component DefaultAuthenticationSuccessHandler, to be more specific - the determineTargetUrl method.

这段代码负责url,会话结束后

This piece of code is responsible for the url, after session ends

if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
            $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');

           return $targetUrl;
        }

那么,让我们扩展这个类并修改 $targetUrl 值.

So, let's extend this class and modify the $targetUrl value.

首先,创建处理程序,我添加了 AuthenticationHandler.phpVendor/YourBundle/Handle 目录

Firstable, create the handler, I have added the AuthenticationHandler.php in the Vendor/YourBundle/Handle directory

<?php

namespace Vendor\YourBundle\Handler;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\ParameterBagUtils;

class AuthenticationHandler extends DefaultAuthenticationSuccessHandler
{
    protected function determineTargetUrl(Request $request)
    {
        if ($this->options['always_use_default_target_path']) {
            return $this->options['default_target_path'];
        }

        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
            return $targetUrl;
        }

        if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
            $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');

            $arr = explode('//', $targetUrl);
            $arr[1] = explode('/', $arr[1]);
            $arr[1][0] .= "/#";
            $arr[1] = implode('/', $arr[1]);
            $arr = implode('//', $arr);

            return $arr;
        }

        if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
            return $targetUrl;
        }

        return $this->options['default_target_path'];
    }
}

注册服务:

#services.yml

services:
    authentication_handler:
        class: Vendor\YourBundle\Handler\AuthenticationHandler
        arguments:  ["@security.http_utils", {}]
        tags:
            - { name: 'monolog.logger', channel: 'security' }

定义处理程序:

#security.yml

     form_login:
         success_handler: authentication_handler

享受吧!

这篇关于重新登录后修改symfony重定向路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆