Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel(PHP)中使用JWT(JSON Web令牌)进行身份验证_Php_Laravel_Jwt_Jwt Auth - Fatal编程技术网

在Laravel(PHP)中使用JWT(JSON Web令牌)进行身份验证

在Laravel(PHP)中使用JWT(JSON Web令牌)进行身份验证,php,laravel,jwt,jwt-auth,Php,Laravel,Jwt,Jwt Auth,我正在尝试使用tymondesigns/jwtauth在Laravel中实现身份验证 (JSON Web令牌) 下面是我的auth.php配置文件中的一些代码: ... 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ], 'guards' => [ 'api' => [ 'driver' => 'jwt', 'provider' =

我正在尝试使用tymondesigns/jwtauth在Laravel中实现身份验证 (JSON Web令牌)

下面是我的auth.php配置文件中的一些代码:

...

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
],
...
下面是我的登录routes/api.php文件中的一个路由:

Route::post('/login', 'AuthController@login');
class AuthController extends Controller
{

    public function login() {
        // $credentials = request(['email', 'password']);
        $credentials = ["email" => "test@test.te", "password" => "test123"];

        if (! ($token = auth()->attempt($credentials))) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type'   => 'bearer',
            'expires_in'   => auth()->factory()->getTTL() * 60
        ]);
    }

    ...
}
以下是我的AuthController.php文件:

Route::post('/login', 'AuthController@login');
class AuthController extends Controller
{

    public function login() {
        // $credentials = request(['email', 'password']);
        $credentials = ["email" => "test@test.te", "password" => "test123"];

        if (! ($token = auth()->attempt($credentials))) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type'   => 'bearer',
            'expires_in'   => auth()->factory()->getTTL() * 60
        ]);
    }

    ...
}
这是我的模型类User.php:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'user';

    protected $primaryKey = 'id';
    public $incrementing = true;
    public $timestamps = false;

    protected $hidden = [
        'password', 'remember_token',
    ];

    ...

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function setPasswordAttribute($password)
    {
        if ( !empty($password) ) {
            $this->attributes['password'] = bcrypt($password);
        }
    }

}

有些人似乎在使用
auth()
时遇到问题。如果您使用
Auth::guard()
Auth::guard('api')
是否有效?@Dan我尝试了所有这些,但仍然得到相同的结果。登录是否与默认的web guard一起工作?因此我将默认的guard更改为“web”,我清除了配置缓存,但仍然不起作用。那么这不是JWT包的错,还有其他问题。打开Tinker with
php artisan Tinker
并使用
bcrypt('test123')
再次散列密码,然后将散列保存到数据库并尝试再次登录。有些人似乎在使用
auth()
时遇到问题。如果您使用
Auth::guard()
Auth::guard('api')
是否有效?@Dan我尝试了所有这些,但仍然得到相同的结果。登录是否与默认的web guard一起工作?因此我将默认的guard更改为“web”,我清除了配置缓存,但仍然不起作用。那么这不是JWT包的错,还有其他问题。打开Tinker with
php artisan Tinker
并使用
bcrypt('test123')
再次散列密码,然后将散列保存到数据库并尝试再次登录。