这里有cakephp版本3.7.2。
Cakephp 在浏览器中将 Csrf 设置为 cookie,但我们希望从没有要设置 cookie 的移动应用程序 [Android] 调用 Api。
我通过评论成功在我的localhost中禁用了Csrf:
$routes->applyMiddleware('csrf'); //configs/routes.php
之后 cookie 不会自动设置。
我的问题是当我将其发布到服务器(在线)时,项目 cookie 仍然在浏览器中设置。
注意:我们使用SSL域名(https)
在 Cakephp 中要禁用 CSRF 中间件,您必须在
CsrfProtectionMiddleware
中注释
/src/Application.php
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(new AssetMiddleware([
'cacheTime' => Configure::read('Asset.cacheTime')
]))
// 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. // comment these lines
// ->add(new CsrfProtectionMiddleware([
// 'httpOnly' => true
// ]));
return $middlewareQueue;
}
Cakephp -> 中间件 -> 跨站请求伪造(CSRF)中间件
希望这会有所帮助!
我知道这是5年前的事了。今天刚遇到,不过我可能会帮助其他遇到同样问题的人:
我使用这个解决方案:
仍然遇到这个问题。解决问题的是你需要清除缓存。
通过终端:bin/cake 缓存clear_all