【问题标题】:Laravel 4 authentication token doesn't matchLaravel 4 身份验证令牌不匹配
【发布时间】:2014-06-17 15:58:34
【问题描述】:

在过滤器文件中,我有以下内容:

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('user/login');
});

这些是我在 routes.php 文件中的路线:

Route::group(['before' => 'auth'], function ()
{
    Route::resource('section', 'SectionController');
    Route::resource('article', 'ArticleController');
});

Route::controller('user', 'UserController');

UserController 是动作发生的地方。在 UserController 内部,这个方法处理登录表单帖子,它是标准 Laravel 刀片模板,没有使用包:

刀片文件:

    {{ Form::open(['url' => 'user/signin']) }}
{{ Form::token() }}
    <div class="form-group">
        <label>{{ trans('user.email') }}</label>
        <input type="email" name="email" value="" class="form-control">
    </div>

    <div class="form-group">
        <label>{{ trans('user.password') }}</label>
        <input type="password" name="password" value="" class="form-control">
    </div>

    <input type="submit" class="btn btn-primary" value="{{ trans('login') }}">
                {{ Form::close() }}

这是 UserController 发布操作:

public function postSignin()
{
    //
    if (Auth::attempt(['email' => Input::get('email'), 'password' => Input::get('password')]))
    {
        return Auth::user()->email;
    }
    else
    {
        return Redirect::to('user/login')->with('message', trans('login.failure'));
    }
}

这是我使用的迁移文件:

public function up()
{
    //
    Schema::create('users', function ($table) {
        $table->increments('id');
        $table->string('email', 16)->unique();
        $table->string('password', 255);
        $table->timestamps();
    });
}

但是当我登录时,我得到一个异常:

Illuminate \ Session \ TokenMismatchException

在filters.php文件中抛出:

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

我做错了什么? Auth::attempt 是否对密码进行哈希处理?它在我用来生成 root 用户的 Seeder 中进行了哈希处理。当我转储 Session::token() 时,它与我的 Form::token() 相同,但仍然会在 filters.php 文件中引发 TokenMismatchException。

更新 我禁用了 csrf 过滤器以便能够实际看到令牌。在我提交表单之前,两个令牌 Session::token() 和 Form::token() 是相同的,我通过查看 HTML 源代码检查了这一点。当我提交表单并在我的 postSignin 方法中使用 dd() 转储令牌时, Session::token() 已更改。它不再与 HTML 源代码中看到的 Session::token() 相同。

return array(
    'driver' => 'array',
);

本地文件夹中的会话配置。

【问题讨论】:

  • 请同时发布您的刀片文件。如果你使用{{ Form::open() }},csrf隐藏字段是自动添加的,如果是手动创建的则不会,除非你添加了;但是我们需要看看你是如何格式化的。
  • @Luceos 刀片文件已添加。标准刀片模板,csrf自动设置。
  • 哦,我明白了;此错误是由“香草”laravel 引起的?
  • 您在使用 APC(请参阅:stackoverflow.com/questions/20129864/…
  • 是的,香草 laravel。我没有使用 APC。除了文档中描述的 local/database.php 之外,我没有更改设置中的任何内容。

标签: php laravel-4


【解决方案1】:

仅供参考 - 这不是身份验证问题 - 它与登录身份验证无关。

由于表单提交,这与 CSRF 令牌有关。在您的代码中的某处,您必须调用 CSRF 过滤器。

将此添加到您的表单应该可以解决问题:

{{ Form::open(['url' => 'user/signin']) }}
    {{ Form::token() }}

    ....  /// rest of form stuff here 

{{ Form::close() }}

编辑:确保您的会话配置也正确。如果它设置为“数组”,它将不起作用。它应该是“文件”或其他选项。

【讨论】:

  • 我将此添加到登录表单中,仍然抛出相同的异常 TokenMismatchException。检查页面的源代码,当我添加 Form::token() 时,我得到了 2 个相同的令牌 - 它已经存在,由 Laravel 添加。
  • 您的会话有问题吗?你的会话配置是什么?
  • 会话被设置为数组,难怪它在请求之间不一样,它不是持久的。
  • 请更新答案以包含配置问题,以防其他人访问此页面。或者自己回答。
【解决方案2】:

经过更多的摆弄后,我意识到解决方案是对密码进行哈希处理。在 Laravel 4.1 中,必须对密码进行哈希处理才能使 Auth 起作用。

在数据库播种器中为密码添加哈希,我可以登录

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2018-11-22
    • 1970-01-01
    • 2013-09-22
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多