dongyan5815 2017-04-06 01:29
浏览 42
已采纳

Laravel 5.3 POST和tokenmismatch

I have a page that I want to have a POST sent to which will trigger a database update. The request will come from another site.

Route::post('update', 'DatabaseController@update');

The request includes a single element

(raw)

id=12345

(form-data)

id: 12345

Whenever I access the page, I get this error

TokenMismatchException in VerifyCsrfToken.php line 68:
  • 写回答

1条回答 默认 最新

  • duanjie9630 2017-04-06 01:55
    关注

    Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user.

    Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.

    Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the csrf_field helper to generate the token field:

    <form method="POST" action="/profile">
        {{ csrf_field() }}
        ...
    </form>
    

    In addition to checking for the CSRF token as a POST parameter, the VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. You could, for example, store the token in a HTML meta tag:

    <meta name="csrf-token" content="{{ csrf_token() }}">
    

    Then, once you have created the meta tag, you can instruct a library like jQuery to automatically add the token to all request headers. This provides simple, convenient CSRF protection for your AJAX based applications:

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    

    Reference

    Note: To exclude some URI's from CSRF verification go to project/app/Http/Middleware, open the file VerifyCsrfToken.php and pass your url like:

    protected $except = [
        '/api/authuser',
    ];
    

    the url passed in this array are excluded from URI's from CSRF verification.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄