dopuz8728 2015-01-16 15:53
浏览 40
已采纳

Symfony2登录use_referer无效

Here's my security.yml for user login:

user_secured_area:
        pattern: /*
        anonymous: ~
        provider: user
        form_login:
            check_path: /login_check
            login_path: /login
            use_referer: true
            username_parameter: _email
        logout:
            path: logout
            target: /

I've checked the profiler for HTTP_REFERRER and i've got the correct referer. However upon login, it is redirected to the root URL and not the referer URL. Any idea on this or have i missed out something?

The controller:

public function indexAction(Request $request){

    $session = $request->getSession();

    //get login error
    if($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)){
        $error = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
    }
    else if(null!==$session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)){
        $error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
        $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
    }
    else{
        $error = "";
    }

    //repopulate username
    $lastEmail = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME); // a ? true : false

    return $this->render('BazaarBundle:Login:login.html.twig', array('last_email' => $lastEmail, 'error' => $error));
}

The twig:

<form action="{{ path('login_check') }}" method="post">
<label for="email">Email:</label>
<input type="text" id="email" name="_email" value="{{ last_email }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<button type="submit">login</button>

  • 写回答

1条回答 默认 最新

  • dongliang1223 2015-01-16 16:35
    关注

    The default behavior is to redirect on the asked page after login.

    The use_referer option is used only if no URL is stored in session. Otherwise, you're redirected to the default target path (which can be defined by using the default_target_path option) or the default page ('/') if no parameters defined.

    Please look into the Session Attributes ine the Symfony debugger (when you're on the login page). Do you have a _security.yourfirewall.target_path record ? What is the value ?

    EDIT: It seems that the session variable is only defined if you're automatically redirected to the login page (for exemple when trying to acces to a secured area without the required rights).

    Another possibility is to add an hidden field named _target_path with the wanted value (exemple below).

    Adding target_path by the form (maybe it should be good to check if it's not empty):

    <input type="hidden" name="_target_path" value="{{ app.request.headers.get('referer') }}" />
    

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题