0

我使用 symfony2 开发了一个应用程序,我想知道如何为以下情况编写 preExecute() 函数 symfony2:当我登录系统时;当我从同一屏幕注销时,它会将我重定向到用户个人资料部分,它会终止会话并在登录屏幕上重定向我,但是当我点击浏览器的后退按钮时,它将在个人资料屏幕上重定向我,这会显示我所有的用户信息,但是当我从同一屏幕单击下一个过程,然后在登录页面上重定向我。

我只想为这种情况添加像 symfony1.4 这样的 preExecute 函数,所以我检查了会话,如果它为空,那么如果我在从系统注销时点击浏览器的后退按钮,它将重定向到登录页面。

我已经在 profileController.php 文件中添加了以下代码,

public function indexAction() {
    $session = $this->get('request')->getSession();
    $userId = $session->get('id');

    if ($userId == 0 || $userId == '') {
        return $this->redirect($this->generateUrl('_security_login'));
    } else {
        //my code 
    }
}

//logout action
public function dologoutAction(){
   $this->get('security.context')->setToken(null);
   $this->get('request')->getSession()->invalidate();
   $this->container->get('request')->getSession('session')->clear();
   return $this->redirect($this->generateUrl('_security_login'));
}

如果有任何其他方式来处理这种情况,那么请帮助我。谢谢你。

4

1 回答 1

0

只需要ROLE_USER配置文件操作中的角色,防火墙就会执行重定向到登录表单然后重定向返回的内容:

/**
 * @Secure("ROLE_USER")
 */
public function profileAction()
{
    // ...
}

更多信息请阅读 Symfony 书籍的安全章节

于 2012-10-03T09:39:36.577 回答