【问题标题】:Laravel 5.2 CSRF Token expires too quicklyLaravel 5.2 CSRF 令牌过期太快
【发布时间】:2017-08-23 18:42:14
【问题描述】:

我正在开发 Laravel 5.2 应用程序,我的所有观点都面临着这个问题。

CSRF 令牌过期太快。其实我只是在忙着填表,一提交就得到TokenMismatchException异常。

我尝试在谷歌上搜索问题,找到了一些类似问题的 git,甚至在 Laracast 上尝试了类似问题,但没有任何成功。

我的 .env 文件有如下几行:

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

这是我的登录方式:

public function postLogin(CookieJar $cookieJar, Request $request)
    {

        $this->validate($request, [
           'email1' => 'required|email',
            'password' => 'required|string'
        ]);
        if($user = User::whereEmail($request->email1)->first() ) {
            if(Hash::check($request['password'], $user->getAttributes()['password'])) {

                if(!$user->getAttributes()['is_active']) {
                    return redirect('/login')->withErrors('Your Account is not Activated Yet!');
                } else if($user->getAttributes()['is_deleted']) {
                    return redirect('/login')->withErrors('Your Account is Banned!');
                } else {
                    # Success
                    $cookie = Cookie::make('user_id', $user->getAttributes()['id'], 864000);
                    return redirect('/')->with('message', 'You have Successfully Logged In!')->withCookie($cookie);
                }
            } else {
                return redirect('/login')->withErrors('Your Login Information is Wrong!');
            }
        } else {
            return redirect('/login')->withErrors('Your Login Information is Wrong!');
        }
    }

请帮帮我。

添加 .env 文件内容和 postLogin 方法。

【问题讨论】:

  • 你为什么不使用 Laravel Auth 功能来处理你的登录验证等?
  • 登录码是别人的密码。
  • 我发现的一个技巧是当我从 .env 文件中删除 SESSION_DRIVER=file 行时,它工作正常。
  • 这不是黑客行为。 Laravel 只是恢复到默认会话驱动程序。
  • 是的,我的意思是这与此有关吗?

标签: laravel-5 token csrf


【解决方案1】:

您可能会遇到会话未保存在您的环境中的问题。如果会话保存到文件,也许做 php artisan cache:clear 或手动清理和重置 storage/framework/sessions/ 中的权限。

我假设您已经使用 csrf_token() 或提供它的东西正确地将令牌添加到您的表单中。

【讨论】:

  • 是的,我已将 csrf_token() 添加到我的每个 POST 方法表单中。
  • 我已经更新了问题的内容,请检查这是否可以帮助您更好地跟踪问题。
  • 存储路径上的权限可能是个问题。确保您的 Web 服务在 storage/ 中的所有文件夹中具有完全的写入和读取权限
猜你喜欢
  • 2017-10-18
  • 2016-12-10
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 2016-05-08
  • 2015-05-09
  • 1970-01-01
  • 2016-10-10
相关资源
最近更新 更多