本当は設定を更新したかったが、方法が見つからず。。
$middlewareQueue->get(3) からできるかなと思ったけど無理だよね。。
てか名前も指定できるようになってたらいいのに。。
->add した瞬間にこれ実行してるのかな。。
まーとりあえず以下の方法で対応しました。
public function middleware($middlewareQueue)
{
$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
->add(ErrorHandlerMiddleware::class)
// Handle plugin/theme assets like CakePHP normally does.
->add(AssetMiddleware::class)
// Add routing middleware.
// Routes collection cache enabled by default, to disable route caching
// pass null as cacheConfig, example: `new RoutingMiddleware($this)`
// you might want to disable this cache in case your routing is extremely simple
->add(new RoutingMiddleware($this, '_cake_routes_'))
// Add csrf middleware.
->add(new CsrfProtectionMiddleware([
'cookieName' => 'csrf',
'secure' => true,
'httpOnly' => true,
]));
return $middlewareQueue;
}
上記がデフォルトの内容となりますが、これから
// Add csrf middleware.
->add(new CsrfProtectionMiddleware([
'cookieName' => 'csrf',
'secure' => true,
'httpOnly' => true,
]));
の部分を削除します。
public function middleware($middlewareQueue)
{
$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
->add(ErrorHandlerMiddleware::class)
// Handle plugin/theme assets like CakePHP normally does.
->add(AssetMiddleware::class)
// Add routing middleware.
// Routes collection cache enabled by default, to disable route caching
// pass null as cacheConfig, example: `new RoutingMiddleware($this)`
// you might want to disable this cache in case your routing is extremely simple
->add(new RoutingMiddleware($this, '_cake_routes_'));
return $middlewareQueue;
}
するとこうなる。
/src/Application.php で分岐させても良いけど、Pluginのための分岐なので、
分岐はPlugin側でやる事に。
/plugins/Manage/config/bootstrap.php
ちなみに /plugins/Manage は管理画面用に作成したプラグイン。
plugins/Manage/config/bootstrap.php を更新。
以下の分岐を作成していたので、その中で
use Cake\Event\EventManager;
use Cake\Http\Middleware\CsrfProtectionMiddleware;
$cookieName = 'csrf';
if (!empty($_SERVER['REQUEST_URI']) && preg_match('/^\/manage*/', $_SERVER['REQUEST_URI'])) {
$cookieName = 'mcsrf';
}
EventManager::instance()->on(
'Server.buildMiddleware',
function ($event, \Cake\Http\MiddlewareQueue $middlewareQueue) use ($cookieName) {
$middlewareQueue->add(new CsrfProtectionMiddleware([
'cookieName' => $cookieName,
'secure' => true,
'httpOnly' => true,
]));
});
のように更新すればOK。
で、確認してたらどうも csrf、mcsrf の両方が登録されてしまう。
何故なんだろうとハマっていたら DebugBar のAjax?で呼んでいる様子。
なので、 DEBUG false にして確認したら無事 /manage 以下では mcsrf だけが利用されており、 /manage と それ以外 で別のクッキー値を参照するようになりましたとさ。