【问题标题】:cakephp referrer after auth deny认证拒绝后的cakephp推荐人
【发布时间】:2013-09-19 17:31:08
【问题描述】:

如何使用 CakePHP 2.x 获取被 Auth 组件拒绝访问的页面?如果我使用 referer() 函数,它会给我链接到被拒绝操作的页面。这是我的代码:

public function login() {
    //get the current url of the login page (or current controller+action)
    $currentLoginUrl = "/login";

    if ($this->request->is('post')) {

        $this->User->recursive = -1;            
        $user = $this->User->find(
            'first', 
            array(
                'conditions' => array(
                    'User.username' => $this->request->data['User']['username'], 
                    'User.password' => AuthComponent::password($this->request->data['User']['password'])
                )
            )
        );

        if ($user && $this->Auth->login($user['User'])) {                   
            //if the referer page is not from login page, 
            if( $this->referer() != $currentLoginUrl  )                 
            //use $this->referer() right away
            $this->redirect($this->referer('/admin', true));  //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead                   
            else
            //if the user lands on login page first, rely on our session 
            $this->redirect( $this->Session->read('beforeLogin_referer') );
        }
        else 
            $this->Session->setFlash('Username or password is incorrect', 'default', array('class' => 'alert-danger'));         
    }

    if( $this->referer() != $currentLoginUrl  )             
    //store this value to use once user is succussfully logged in
    $this->Session->write('beforeLogin_referer', $this->referer('/admin', true) ) ;  //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead                
}

所以基本上发生的事情是我没有登录,我在这个网址:

'http://localhost/hotelguide/hotels/view/17/'

然后我点击一个链接会带我去

'http://localhost/hotelguide/hotels/review/17/'

但这需要用户登录,所以它将我重定向到登录页面,当我调试referrer()时,它给了我这个:

'http://localhost/hotelguide/hotels/view/17/'

我做错了什么?

【问题讨论】:

  • 为什么不使用 CakePHP 框架提供的 Auth 和登录?会将这 30 行减少到 4 行。不要与框架抗争。
  • 因为它不能满足我的需要。它不会正确地返回
  • 在这种情况下,您希望您的推荐人是什么?
  • 应该是http://localhost/hotelguide/hotels/review/17/
  • 请检查我的回答。如果有用,请将答案设置为已接受或投票。否则,只需写下答案中缺少的内容或您现在正在处理的问题,我会尽力提供帮助。

标签: cakephp authentication


【解决方案1】:

当您在 CakePHP 中使用 Auth 组件并尝试访问受限站点时,它会将您重定向到登录页面并将引用页面保存在会话中。会话密钥 Auth.redirect 包含您正在寻找的值 - 您尝试访问的页面。

看一下AuthComponent的__unauthenticated()方法。它包括负责将会话值写入Auth.redirect 的代码。如果你不想使用AuthComponent,你可以查看它在组件中是如何实现的,并根据我提到的方法编写自己的解决方案。

【讨论】:

    【解决方案2】:

    $this->referer() 不会为您提供正确的推荐人网址。如果你想获取referrer url,只需使用 $this->Session->read('Auth.redirect');

    您可以通过 $this->Session->read('Auth.redirect'); 找到您要查找的确切 url;

    $this->每次重新加载页面时都会更新referer()值。

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 2010-11-19
      • 2011-04-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 2023-04-01
      • 2012-06-18
      相关资源
      最近更新 更多