【问题标题】:CSRF TOKEN MISTATCH LARAVELCSRF 代币误发 LARAVEL
【发布时间】:2021-06-16 17:43:56
【问题描述】:

我在 laravel 中遇到 CSRF 令牌不匹配问题。我也检查了来自 stackflow 和其他站点的所有注释,但无法清除此问题。

https://mybestlife.mg-wellness.com/admin/login

用户:admin@gmail.com 通过:1234

您可以在控制台中检查错误。


阿贾克斯

$("#loginform").on('submit', function(e){

  e.preventDefault();
  $('button.submit-btn').prop('disabled', true);
  $('.alert-info').show();
  $('.alert-info p').html('Authenticating...');

  $.ajax({
      type:"POST",
      url:$(this).prop('action'),
      data:new FormData(this),
      headers: headers,
      dataType:'JSON',
      contentType: false,
      cache: false,
      processData: false,
      success: function(data) {
          console.log(data);
          if ((data.errors)) {
              $('.alert-success').hide();
              $('.alert-info').hide();
              $('.alert-danger').show();
              $('.alert-danger ul').html('');
              for(var error in data.errors) {
                  $('.alert-danger p').html(data.errors[error]);
              }
          } else {
              // console.log(data)
              $('.alert-info').hide();
              $('.alert-danger').hide();
              $('.alert-success').show();
              $('.alert-success p').html('Success !');
              window.location.href = data;
          }
          $('button.submit-btn').prop('disabled',false);
      }
  });
}); 

登录功能

public function doLogin(Request $request)
{
    // print_r($request->all());
    $rules = [
        'email'    => 'required|email',
        'password' => 'required'
    ];
    
    $validator = Validator::make($request->all(), $rules);
            
    if ($validator->fails()) {
        return response()->json(array('errors' => $validator->getMessageBag()->toArray()));
    }
    //--- Validation Section Ends
    
    // Attempt to log the user in
    if (Auth::guard('web')->attempt([
        'email' => $request->email,
        'password' => $request->password,
        'status' => 1,
        'role' => 1,
        'level' => 0
    ], $request->remember)) {
        // if successful, then redirect to their intended location
        return response()->json(route('dashboard'));
    }
    
    // if unsuccessful, then redirect back to the login with the form data
    return response()->json(array('errors' => [
        0 => 'Credentials Doesn\'t Match !'
    ]));    
}

代码在 localhost 和我的测试服务器上运行良好。但不是在服务器上,我在上面分享。 请帮我解决这个问题。

谢谢

【问题讨论】:

  • 检查您的生产服务器时间。可能不同步
  • 能发一下htaccess文件代码吗
  • 我可以看到令牌正在生成每个请求,因此它会抛出错误。尝试重新启动服务器或检查 htaccess 文件或禁用 cookie 等。
  • 服务器禁用 cookie 吗?

标签: laravel csrf


【解决方案1】:

我认为您缺少标题。这就是我在使用 ajax 时总是传递 CSRF 令牌的方式。

以下内容应在您的resources/js/bootstrap.js 文件中。

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

尝试使用您的网站登录时,我在请求中看不到 X-CSRF-TOKEN 标头。


我认为您没有正确设置APP_URL 的值。它应该是 APP_URL=https://mybestlife.mg-wellness.com 而不是你目前拥有的。 (APP_URL=http://localhost/mybestlife_mg_wellness/)

(更改.env文件后运行php artisan config:clear

您的站点已打开调试模式,并且与路由 'user_login' 不存在有关的无关错误,这会暴露您的整个配置。请注意更新您的数据库凭据,它们已受到威胁

这很重要。

另外,重新运行命令php artisan key:generate 以更新APP_KEY

【讨论】:

  • var headers = { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } 先生已经存在了跨度>
  • 好吧,我想我知道它是什么了。我会编辑答案
  • 全部在本地完成并再次上传。还是一样的问题:(
【解决方案2】:

您必须在表单中添加@csrf

<form  method="post" action="javascript:void(0)" enctype="multipart/form-data">
                    @csrf 
</form>

在 jquery 中也添加标题

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

在页面头部添加这个元标记

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

那么您将永远不会收到令牌错误。

【讨论】:

  • 你必须提供你的表格还有什么是你必须添加的错误然后我会帮助你得到你的解决方案
  • 这是mybestlife.mg-wellness.com/admin/login 的形式,控制台出现错误。还需要什么吗?
【解决方案3】:

在数据数组中添加下面一行

 data:{
    _token : {{csrf_field()}},
     
     name:name
  }

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2014-05-29
    • 2012-07-02
    • 2015-09-21
    相关资源
    最近更新 更多