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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?