1

私はすべてのページにsslを使用する必要があるcakephpサイトを開発しました。コントローラでリダイレクトを使用すると、https://subdomain.domain.com/controller/actionではなくhttp://subdomain.domain.comにリダイレクトされることを除いて、期待どおりに機能します。

私は、cakephpアプリケーションを指すポート80の仮想ホストを作成し、これらの書き換えルールを.htaccessに追加することで、これを解決しました。

RewriteCond%{HTTPS} off RewriteRule(。*)https://% {HTTP_HOST}%{REQUEST_URI} [L]

これにより、この状況がキャッチされ、httpsにリダイレクトされますが、サーバーに不要な余分なトラフィックが発生します。

この余分なトラフィックの原因は、間違ったURLを生成するリダイレクト機能です。リダイレクト関数を調べたところ、router::urlを呼び出して実際のURLを作成しました。ただし、httpではなくhttpsを使用するようにルーターに指示する方法や場所がわかりません。

ティム

4

3 に答える 3

5

I'm taking a bit of a guess, but I suspect it is this very RewriteRule that's messing things up.

You should be "redirecting" not "rewriting". Cake's generated links are usually relative to the root, so don't specify a protocol unless you pass "true" as the second parameter.

I also have apache listening on both 80 and 443 so that I can at least respond to incorrect requests.

This is the code I have in my AppController class to do the same thing:

function beforeFilter() {
    parent::beforeFilter();
    $this->_setupSecurity();
}

function _setupSecurity() {
    $this->Security->blackHoleCallback = '_badRequest';
    if(Configure::read('forceSSL')) {
        $this->Security->requireSecure('*');
    }
}

/**
* The main SecurityComponent callback.
* Handles both missing SSL problems and general bad requests.
*/

function _badRequest() {
    if(Configure::read('forceSSL') && !$this->RequestHandler->isSSL()) {
        $this->_forceSSL();
    } else {
        $this->cakeError('error400');
    }
    exit;
}

/**
* Redirect to the same page, but with the https protocol and exit.
*/

function _forceSSL() {
    $this->redirect('https://' . env('SERVER_NAME') . $this->here);
    exit;
}

I also have my own config 'forceSSL' option in bootstrap.php for turning this on and off depending on the environment, so that needs to be set to true for the above to work.

于 2010-12-17T17:30:13.867 に答える
0

その例は、クックブックhttp://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#usageで既に言及されています

class AppController extends Controller {
    // Add security component
    public $components = array('Security');

    public function beforeFilter() {
        $this->Security->blackHoleCallback = 'forceSSL';
        $this->Security->requireSecure();
    }

    // Add this function in your AppController
    public function forceSSL() {
        return $this->redirect('https://' . env('SERVER_NAME') . $this->here);
    }
}
于 2014-11-19T05:44:46.060 に答える
0

エラーは Apache の設定ミスであることがわかりました。

Jamies ソリューションを試してみると、リクエストが https であっても RequestHandler->isSSL() が false を返すため、サイトはリダイレクト ループに陥りました。次に、$_SERVER['https'] が設定されておらず、$_SERVER['port'] が予想どおり 443 ではなく 80 であることを発見しました。

その時点で、ssl ディレクティブを sites-available/default に配置しました。

SSLEngine on
SSLCertificateFile [path to cert file]
SSLCertificateKeyFile [path to keyfile]

ssl ディレクティブをサブドメインの仮想ホストに移動すると、リダイレクト ループの問題が解決されました。

実際には、メソッド Router::url が $_SERVER['https'] をチェックするため、初期の問題も解決されます。設定されている場合、https: で始まる URL が生成されます。

Jamies のソリューションと自分のソリューションの両方を .htaccess の書き換えルールでテストしましたが、修正後は両方とも期待どおりに動作します。

ティム

于 2010-12-19T15:20:53.907 に答える