1
我使用'安全'組件,但我想爲它創建一個'標誌'(基本上,所以它在本地和測試服務器上被禁用,但在活動服務器上啓用)。以下是我想出了(爲了保持這個例子簡單,我們只需將其應用到在AppController的整個網站):CakePHP SSL標誌
bootstrap.php中:
Configure::write('enableSSL', false);
AppController.php:
public $components = array('Security');
public function beforeFilter() {
parent::beforeFilter();
$enableSSL = Configure::read('enableSSL');
if(isset($enableSSL) && $enableSSL == true) {
$this->Security->blackHoleCallback = 'forceSSL';
$this->Security->requireSecure('shipping_billing', 'payment');
}
}
function forceSSL() {
// only redirect if they aren't viewing the site via https
$enableSSL = Configure::read('enableSSL');
if(isset($enableSSL) && $enableSSL == true) {
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
} else {
$this->redirect('https://' . $_SERVER['SERVER_NAME'] . $this->here);
}
}
}
但是,通過上面的代碼,當試圖訪問某些頁面(或提交表單)時,出現404錯誤。任何想法爲什麼?我已經在beforeFilter/forceSLL中檢查了$ enableSSL的值,並且它是false)。它與包含組件有關;如果我刪除所有其他代碼,但保留公共$ components = array('Security');行,我仍然得到404頁錯誤...
當包括安全組件,它會自動使用其CSRF功能,這可能是問題的根源。通過CSRF檢查,如果您使用Cake的表單助手以外的其他方式創建表單,或者如果您嘗試通過ajax提交,表單將被拒絕。很難說,但是如果不知道更多關於那些不工作的網頁的信息,你可以做些什麼來修復它。簡單的方法就是嘗試在這些頁面上禁用CSRF保護。 http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#disabling-the-csrf-protection – Kai