【问题标题】:CakePHP API Blackhole - validatePost disable not working?CakePHP API Blackhole - validatePost 禁用不起作用?
【发布时间】:2012-09-05 06:30:42
【问题描述】:

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

但是,既然这样做了,我们就遇到了 Blackholes 的实际问题。尽管

$this->Security->validatePost = false;  

在 AppController.php 中设置

我们使用的是 CakePHP 2.1.3 版

代码示例如下:

AppController.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”请求工作正常(不是黑洞)。

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

【问题讨论】:

  • Cake Core 已更新至 2.2.2,但问题仍然存在。我还尝试在整个站点范围内禁用安全性: $this->Security->enabled = false;但是 https POST 请求的黑洞仍然存在。

标签: php api cakephp rest ssl


【解决方案1】:

原来以下内容在 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;
    }
}

【讨论】:

  • 我在这方面花了很多时间,你的解决方案终于对我有用。我还阅读了以下文档,并且正确使用了$this->Form->create();$this->Form->end();。感谢您分享您的解决方案!
  • 很遗憾,所有关于 SO 的答案都等于关闭安全性。不幸的是,安全组件是 CakePHP 最薄弱的部分
【解决方案2】:

见以下网址

CakePHP: Disable Security Component site wide

Disabling input elements in a CakePHP form that uses Security component and jQuery

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;

    }

}

【讨论】:

  • 嗨。感谢您的回复。我不想禁用安全性,因为我想强制用户使用 HTTPS(我相信在启用安全性时会强制这样做?)此外,禁用安全性似乎是解决此问题的一种锤子方法 - 问题实际上与 Form验证,对吗?
  • 可能这个问题确实与表单验证有关。你可以看到这两个 url book.cakephp.org/2.0/en/core-libraries/components/… api.cakephp.org/class/security-component API
  • 感谢您的回答,但这并不能回答我的问题。我已经阅读了这两个文档,但是它们提供的信息并不能解决我的问题。我还尝试在 AppController 中全局禁用安全性,但请求仍然是黑洞——这开始看起来像一个 CakePHP 错误。
猜你喜欢
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多