【问题标题】:disable csrf for unprotect method post, put and delete in laravel 5在 laravel 5 中禁用 csrf 以取消保护方法发布、放置和删除
【发布时间】:2015-10-19 20:52:23
【问题描述】:

在 laravel 4 中默认不使用 csrf 保护方法(POST、PUT 和 DELETE),但在 larave 5 中使用 csrf 保护 post、put 和 delete 方法免受注入代码的影响是默认的。这种保护对表单没有问题,但对构建 API 休息有问题。

所以请帮助我展示如何禁用 csrf 取消保护方法(POST、PUT 和 DELETE)以在 laravel 5 中构建 api rest。谢谢

【问题讨论】:

    标签: laravel-routing


    【解决方案1】:

    转到应用程序->http->内核

    打开内核文件:

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
      //  \App\Http\Middleware\VerifyCsrfToken::class, // this is the csrf token, just disable using '//'
    ];
    

    【讨论】:

      【解决方案2】:

      这是来自here的方法,我测试过应该没问题。

      简而言之,要在特定页面禁用 csrf,只需将 app/Http/Middleware/VerifyCsrfToken.php 更改为如下内容:

      public function handle($request, Closure $next)
          {
              //disable CSRF check on following routes
              $skip = array(
                          'user/path/xys',
                          'user/profile',
                          'my/unprotected/route'
                          );
      
              foreach ($skip as $key => $route) {
                  //skip csrf check on route
                  if($request->is($route)){
                      return parent::addCookieToResponse($request, $next($request));
                  }
              }
      
              return parent::handle($request, $next);
          }
      

      【讨论】:

        【解决方案3】:

        如果您只关心/api/* 路线,您可以在Stack Overflow 上关注我的回答

        希望这可以帮助您获得干净简洁的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-30
          • 2017-05-04
          • 2015-02-19
          • 2018-05-27
          • 2015-03-11
          • 2016-01-15
          • 1970-01-01
          • 2015-08-03
          相关资源
          最近更新 更多