我想实现HTTPS的所有网页。
我使用CakePHP 2.3的“验证”的组成部分。
其实我发现的唯一一个办法就是添加一个“beforeFilter条件”。
但是,这是非常肮脏的,因为我有很多,因为验证组分的“非SSL”的请求。
AppController.php --->
class AppController extends Controller {
public function beforeFilter() {
if (env("SERVER_PORT") != "443") {
$this->Security->blackHoleCallback = 'forceSSL';
$this->Security->requireSecure();
}
}
public function forceSSL() {
$this->redirect('https://' . env('SERVER_NAME') . $this->here);
}
}
问题出现时,我没有登录,我尝试访问我的网站
- 请求- >应答(因为)
- GET hxxp:// MYSITE / - > 302 hxxp S:// MYSITE /(beforeFilter重定向)
- GET hxxp S:// MYSITE / - > 302 hxxp:// MYSITE /用户/登录(验证部件)
- GET hxxp:// MYSITE /用户/登录- > 302 hxxp S:// MYSITE /用户/登录(beforeFilter重定向)
- GET hxxp S:// MYSITE /用户/登录- > 200
- POST hxxp S:// MYSITE /用户/登录(与creds) - > 302 hxxp:// MYSITE /(AUTH成分)
- GET hxxp:// MYSITE / - > 302 hxxp S:// MYSITE /
- GET hxxp S:// MYSITE / - > 200
那么,你知道另一种方式来做到这一点。
注:我不得不强迫保护我的cookie在core.php中,因为他们没有。
core.php文件--->
Configure::write('Session', array(
'defaults' => 'php',
'ini' => array(
'session.cookie_secure' => true
)));
请注意,我也试图通过修改的.htaccess强制SSL,但我得到的是无限循环。
编辑:
在CakePHP中的默认的.htaccess是
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
我想补充一下:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]