0

这里我们有 cakephp 版本 3.7.2。

Cakephp 在浏览器中将 Csrf 设置为 cookie,但我们希望从没有设置 cookie 的移动应用程序 [Android] 调用 Api。

我通过评论成功禁用了本地主机中的 Csrf:

$routes->applyMiddleware('csrf'); //configs/routes.php

之后cookie不会自动设置。

我的问题是当我在服务器(在线)上发布它时,项目 cookie 仍然设置在浏览器中。

注意:我们使用 SSL 域 (https)

4

1 回答 1

2

在 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)中间件

希望这会有所帮助!

于 2019-01-20T04:08:29.307 回答