如果login_check失败,则返回503服务不可用状态 - symfony2

时间:2012-08-30 05:57:47

标签: authentication symfony firewall

我正在开发一个使用Symfony2作为框架的Web应用程序,用户需要登录才能使用其余的服务。我正在使用symfony的防火墙对用户进行身份验证。

防火墙代码

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern:  ^/login$
        security: false

    secured_area:
        pattern:    ^/
        form_login: ~
        logout: ~

控制器代码

public function indexAction()
{
    return $this->redirect($this->generateUrl('index_search'));
}

public function loginAction() {

    $request = $this->getRequest();
    $session = $request->getSession();

    // get the login error if there is one
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render('IsBeagleBundle:Auth:login.html.php', array(
        // last username entered by the user
        'last_username' => $session->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    ));
}

路线

$collection->add('index', new Route('/', array(
    '_controller' => 'IsBeagleBundle:Auth:index',
)));

$collection->add('login', new Route('/login', array(
    '_controller' => 'IsBeagleBundle:Auth:login',
)));
$collection->add('login_check', new Route('/login_check', array()));

我面临的问题是

如果防火墙身份验证通过,则重定向工作正常并转到搜索页面,但如果身份验证失败,则返回503 Service Unavailable状态,应该再次进入登录页面。

但如果我刷新空白页面,则会显示登录页面。

任何人都可以弄清楚我做错了什么吗?

编辑1 - 输出

enter image description here

编辑2

如果我在前端控制器app.php上注释掉以下行,

$kernel = new AppCache($kernel);

然后返回的状态更改为200,但是等待时间很长,响应为空。我想通过的另一件事是,如果我尝试在登录控制器中打印symfony会话变量

$request = $this->getRequest();
$session = $request->getSession();

echo '<pre>';
print_r($session);
echo '</pre>';

它需要太多的时间和记忆,永远不会结束。这导致浏览器冻结。我找不出原因。

P.S:我正在使用symfony标准分发

1 个答案:

答案 0 :(得分:0)

尝试从$模式中删除^/login$,以匹配以/login开头的所有内容。