【问题标题】:laravel - CSRF token always changeslaravel - CSRF 令牌总是改变
【发布时间】:2015-08-10 12:51:02
【问题描述】:

这就是我昨天面临的问题。它总是给我TokenMismatchException,当我深入研究并比较一些东西时,我发现在我的本地服务器上,_token 字段永远不会改变。但在我的制作中,确实如此。这就是它不断给我TokenMismatchException的原因。有谁知道如何解决这个错误。

我有

  1. this question
  2. 查看文档。
  3. 编写了几个代码接收测试。
  4. <input id="token" type="hidden" value="{{ csrf_token() }}"> 这已经在我的代码中了。

【问题讨论】:

  • [这里是代码](view-source:cornchat.ga/auth/register)。检查第 132 行。它总是在变化。

标签: php laravel laravel-5 csrf


【解决方案1】:

检查domain 中的config/session.php 设置是否正确。甚至我也遇到了同样的问题。并通过更改路径来解决它。

【讨论】:

  • 谢谢老哥,帮了大忙。
【解决方案2】:

可能有用。

HTML:

<meta name="_token" content="{{ csrf_token() }}">

Js:

var network = {
    post: function(path, params, cb, type){
        $.ajax({
            url: path,
            type: 'post',
            data: params,
            headers: { "X-CSRF-TOKEN" : $('meta[name="_token"]').attr('content') },
            dataType: type,
            success: function (response, status) {
                if (status == "success") {
                    if (response.reason == "token_timeout") {
                        var new_token = response.new_token;
                        $('meta[name="_token"]').attr('content', new_token);
                        network.post(path, params, cb, type);
                    }else{
                        cb(response);
                    }
                }
            }
        });
    }
};

network.post('path to handler...', { key: value... }, function(response){
   if(response.status == 'success'){
       // to do
   }
}, "json");

/app/Exceptions/Handler.php:

    public function render($request, Exception $exception) {

        if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
            return response()->json(['reason' => 'token_timeout', 'new_token' => csrf_token()], 200);
        }

        return parent::render($request, $exception);
    }

【讨论】:

    猜你喜欢
    • 2014-09-10
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2017-10-18
    • 1970-01-01
    • 2018-09-23
    • 2018-09-22
    相关资源
    最近更新 更多