3

由于通过 API 进行纯密码登录,我们最近将应用程序从 http 转移到了 https。

然而,既然这样做了,我们就遇到了黑洞的真正问题。Cake 似乎在我们的控制器中对 API 函数的任何“POST”进行了黑洞处理,尽管

$this->Security->validatePost = false;  

在 AppController.php 中设置

我们使用的是 CakePHP 2.1.3 版

代码示例如下:

应用控制器.php:

function beforeFilter() 
{
    $this->Security->validatePost = false;  
    $this->Security->requireSecure(); 
}

SaleOrderController.php:

function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->allow('addApi');   // Allow access to the API without logging in.
}

发布到此 URL 会返回以下消息:“请求已被黑洞”

一旦我们可以让它工作(没有被黑洞),我们将对其进行调整,以便仅可以使用 validatePost = false 执行某些操作。但是,现在我们只想让系统正常工作。

注意:对操作的“GET”请求工作正常(不是黑洞)。

我在这里遗漏了一些简单的配置还是在工作中存在一些更深层次的问题?文档中的安全模块似乎有点少,从我的谷歌搜索来看,大多数人似乎通过执行与我相同的步骤来避免黑洞。

4

2 回答 2

7

事实证明,以下内容在 CakePHP 2.X 中无效:

$this->Security->enabled = false;

要禁用组件,您需要遵循此文档: http ://book.cakephp.org/2.0/en/core-libraries/components/security-component.html

我的问题与 CSRF 保护有关,我认为这在 CakePHP 2.X 中可能是新的?无论如何,我需要做的就是在我的 SaleOrderController beforeFilter 函数中添加以下行:

$this->Security->csrfCheck = false;

我的整个 BeforeFilter 功能现在是:

function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->allow('addApi');   // Allow access to the API without logging in.
    if (isset($this->Security) && $this->action == 'addApi') {
        $this->Security->csrfCheck = false;
        $this->Security->validatePost = false;
    }
}
于 2012-09-05T18:40:12.110 回答
1

见以下网址

CakePHP:在站点范围内禁用安全组件

在使用 Security 组件和 jQuery 的 CakePHP 表单中禁用输入元素

http://life.mysiteonline.org/archives/175-Disable-the-Security-Component-in-CakePHP-only-for-Certain-Actions.html

http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html

http://api.cakephp.org/class/security-component

或者试试看:-

即使您在 app_controller 中禁用它,您的个人控制器也可能启用了该安全性。正如我的疯狂猜测所说,这就是您想要做的。如果不让我了解更多信息

function beforeFilter(){
    parent::beforeFilter();

    if(isset($this->Security) && $this->RequestHandler->isAjax() && $this->action = 'add'){

        $this->Security->enabled = false;

    }

}
于 2012-09-05T06:39:58.333 回答