【问题标题】:CakePHP 2.0 Basic Authentication always gives 302 redirect instead of 401 unauthorizedCakePHP 2.0 Basic Authentication 总是给出 302 重定向而不是 401 未授权
【发布时间】:2012-03-05 12:29:58
【问题描述】:

我在一个简单的 CakePHP 2.0 应用程序中设置了基本身份验证。我首先将应用程序设置为使用常规表单身份验证,然后将以下行添加到我的 AppController.php 的 beforeFilter() 以启用基本 http 身份验证:

$this->Auth->authenticate = array('Basic');

这是完整的 AppController:

<?php
class AppController extends Controller {

    public $components = array(
            "Session",
            "Auth" => array(
                'loginRedirect' => array('controller'=>'users','action'=>'index'),
                'logoutRedirect' => array('controller'=>'users','action'=>'index'),
                'authError' => "You are not authorized to view this page.",
                'authorize' => array('Controller'),
        )
    );

    public function isAuthorized($user) {
        return true;
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index','view');
        $this->set('logged_in', $this->Auth->loggedIn());
        $this->set('current_user',$this->Auth->user());
        $this->Auth->authenticate = array('Basic');
    }
}
?>

理想情况下,我希望整个应用程序中的一个特定控制器(将公开用于移动设备的 API 的控制器)仅使用基本 HTTP 身份验证,而其余控制器的行为就像普通网络一样应用。

目前,如果我将不正确的凭据传递给控制器​​,我会收到 HTTP 302 响应,而我真的很想传回 HTTP 401。我该怎么做?

*编辑错别字

【问题讨论】:

  • 题外话:您确定要将 Basic 用于移动应用程序吗? Basic 发送未加密的凭据,即使使用 SSL,也很容易被本地代理嗅探。 thinkvitamin.com/asides/…
  • 我同意,这并不理想。我想开始使用 Digest Auth,这是更好的选择吗?

标签: php cakephp basic-authentication


【解决方案1】:

cakePHP 2.x 中,您可以 create a custom status code 例如

if ($this->Auth->login()) {
  return $this->redirect($this->Auth->redirect());
} else {
  throw new MissingWidgetHelperException('You are not authorized to view this page.', 401);
}

【讨论】:

    【解决方案2】:

    我还没有尝试过 2.0,但通常 Auth 所做的是使用 setFlash 设置错误消息,然后将您重定向到某个地方以向您显示该消息。这可能就是您获得 302 重定向的原因。

    手动设置header然后退出怎么样?

    例如

    public function login() {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            header("HTTP/1.0 401 Unauthorized");
            exit;
        }
    }
    

    【讨论】:

      【解决方案3】:
      public function beforeFilter() {
          parent::beforeFilter();
          $this->Auth->allow('index','view');
          $this->set('logged_in', $this->Auth->loggedIn());
          $this->set('current_user',$this->Auth->user());
          if($this->name == 'Specific') {
                  // for the specific controller
                  $this->Auth->authenticate = array('Basic');
          } else {
                  // everything else
          }
      }
      

      查看 KVZ 的其余插件,它可能会感兴趣 https://github.com/kvz/cakephp-rest-plugin

      【讨论】:

      • 这是一个有趣的插件,但不幸的是,您的解决方案没有创建我正在寻找的功能。我无法获得纯 HTTP 身份验证来覆盖默认的基于表单的身份验证。目前,如果我尝试使用 HTTP Basic 身份验证(并且不提供正确的凭据),我会得到 302 重定向而不是预期的 401 或 403
      猜你喜欢
      • 2023-03-07
      • 2015-08-05
      • 2023-04-02
      • 1970-01-01
      • 2019-05-14
      • 2018-04-08
      • 2021-08-03
      • 2016-05-22
      • 2019-04-12
      相关资源
      最近更新 更多