登录时令牌不匹配异常(Laravel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22066241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:06:48  来源:igfitidea点击:

Token Mismatch Exception on Login (Laravel)

laravel

提问by Sainath Krishnan

I followed thistutorial on creating a registration and login page using Laravel.

我按照教程使用 Laravel 创建注册和登录页面。

Everything works smoothly, the only issue is that I am unable to Login. If I provide the wrong username/password, it correctly gives me an error message. But if I use the right credentials, I get the following error -

一切顺利,唯一的问题是我无法登录。如果我提供了错误的用户名/密码,它会正确地给我一条错误消息。但是,如果我使用正确的凭据,则会出现以下错误 -

Illuminate \ Session \ TokenMismatchException

This is my (default) csrf function -

这是我的(默认)csrf 函数 -

Route::filter('csrf', function()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

This is the form action -

这是表单操作 -

{{ Form::open(array('url'=>'signin', 'class'=>'form-signin')) }}

And this is the relevant portion of my UsersController

这是我的 UsersController 的相关部分

public function __construct() {
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth', array('only'=>array('getDashboard')));

}
public function postSignin() {
       if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
            return Redirect::to('dashboard')->with('message', 'You are now logged in!');
        } else {
            return Redirect::to('login')
            ->with('message', 'Your username/password combination was incorrect')
            ->withInput();
    }  
}

public function getDashboard() {
    $this->layout->content = View::make('users.dashboard');
}

回答by tacone

Laravel makes a quite esoteric use of sessions, and when the user cookie and the user salt in stored in the database disalign for some reason (for example, when you re-seed your user table), you get a Token Mismatch Exception without further explanation.

Laravel 对会话的使用非常深奥,当存储在数据库中的用户 cookie 和用户盐由于某种原因(例如,当您重新播种用户表时)不一致时,您会得到一个令牌不匹配异常,无需进一步解释.

If that's the case, just delete your cookies.

如果是这种情况,只需删除您的 cookie。

回答by Radu Pacurar

I had this problem on login also. From time to time this exception occurred so I stop and tried to reproduce it. I succeed by doing this:

我在登录时也遇到了这个问题。不时发生此异常,所以我停下来尝试重现它。我这样做成功了:

First I load the login page.

首先我加载登录页面。

Then I deleted the cookies.

然后我删除了cookies。

Then, without reloading the login page, I entered username and password and tried to login.

然后,没有重新加载登录页面,我输入用户名和密码并尝试登录。

Because session was deleted (when I deleted the cookies), it was normal that this code was not going to pass and it will throw the TokenMismatchException.

因为 session 被删除了(当我删除 cookie 时),这段代码不会通过并且会抛出 TokenMismatchException 是正常的。

Route::filter('csrf', function() {
    if ( Session::getToken() != Input::get('_token')) {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

So, what I've done to solve my problem was to add a redirect to login page with a message to inform the user that the session might expired.

因此,我为解决我的问题所做的工作是添加一个重定向到登录页面的消息,以通知用户会话可能已过期。

Route::filter('csrf', function() {
    if ( Session::getToken() != Input::get('_token')) {
        return Redirect::to('/admin/login')->with('warning', 'Your session has expired. Please try logging in again.');
    }
});

Thus, after page reloading, a new session is created and the problem is solved.

因此,在页面重新加载后,创建了一个新会话并解决了问题。

回答by creed06

/config/session.php set that info 'expire_on_close' => true, save and load again ur site you wont get anymore the issue with Token Mismatch Exception on Login

/config/session.php 设置该信息 'expire_on_close' => true,再次保存并加载您的站点,您将不会再遇到登录时令牌不匹配异常的问题

回答by Ph?m Ti?n Thành

Check your route:list to see if Login is protected by web middleware. In my case, I made mistake by adding web middileware to login route where it should be guest middleware.

检查您的 route:list 以查看 Login 是否受 Web 中间件保护。就我而言,我通过将 web 中间件添加到应该是访客中间件的登录路由而犯了错误。

回答by Vítor ávila

I had this problem, it was caused by no free disk space.

我遇到了这个问题,它是由没有可用磁盘空间引起的。

Get some space and try to login again.

获得一些空间并尝试再次登录。

回答by mike.bronner

I added the following code to app\Exceptions\Handler.phpto help clarify the error for the end-user, in addition to setting session to expire on close, as mentioned in this thread.

我添加了以下代码以app\Exceptions\Handler.php帮助最终用户澄清错误,此外还设置会话在关闭时过期,如本线程所述。

    protected function prepareException(Exception $exception)
    {
        if ($exception instanceof TokenMismatchException) {
            return new HttpException(
                419,
                "{$exception->getMessage()}. Please clear your browser cookies and try again.",
                $exception
            );
        }

        return parent::prepareException($exception);
    }