Laravel基本HTTP身份验证检查返回False [英] Laravel Basic HTTP Auth Check Returning False

查看:293
本文介绍了Laravel基本HTTP身份验证检查返回False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel中提供的基本HTTP身份验证来登录我的网站.但是,当我呼叫Auth::Check()时,即使我已经登录,我也总是得到 false 作为响应.

Auth::Check()不能与基本身份验证模型一起使用,如果不能,是否有任何方法可以检查基本身份验证以查看用户是否已登录?

这是我的用户类别:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }
}

这是我设置身份验证过滤器要使用的代码段

$this->middleware('auth.basic', ['only' => ['create', 'store', 'edit', 'update', 'destroy']]);

这是我的Auth::Check()通话(始终打印0):

public function show($id)
{
    echo \Auth::check() ? '1' : '0';
    die();
    #.......
}

我的路线:

解决方案

它已在5.2版本中更改.

如果您要使用会话,csrf,cookie ext.您应该在自己的路线中使用这样的网络"中间件:

Route::group(['middleware' => ['web']], function () {
//
});

您可以在您的项目中看到新的kernel.php文件,如下所示:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
],

'api' => [
    'throttle:60,1',
],
];

更多信息: https://laravel.com/docs/5.2/releases

I'm using the basic HTTP authentication provided in Laravel to log in to my website. However, when I call Auth::Check() I always get false as the response even though I am logged in.

Does Auth::Check() not work with the basic authentication model and if not, is there any way to check the basic authentication to see if a user is logged in?

This is my user class:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }
}

This is the segment of code where I set the authentication filter to use

$this->middleware('auth.basic', ['only' => ['create', 'store', 'edit', 'update', 'destroy']]);

And this is my Auth::Check() call (Always prints 0):

public function show($id)
{
    echo \Auth::check() ? '1' : '0';
    die();
    #.......
}

My routes:

解决方案

It changed in 5.2 version.

If you will use session, csrf, cookie ext. you should to use "web" middleware like this in your routes:

Route::group(['middleware' => ['web']], function () {
//
});

And you can see in your project the new kernel.php file is like this:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
],

'api' => [
    'throttle:60,1',
],
];

More info: https://laravel.com/docs/5.2/releases

这篇关于Laravel基本HTTP身份验证检查返回False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆