我在 Laravel 8 中使用“tymon/jwt-auth”。我完全按照“https://jwt-auth.readthedocs.io/en/docs/laravel-installation/”中的步驟操作,但我仍然總是得到 401未經授權。
在 Postman 中進行除錯時,我無法弄清楚它的原因,而且我沒有做任何與教程中的人所做的不同的事情。我在網上查了一下,有人懷疑密碼可能不是作為加密字串(md5)讀取的。有什么想法嗎?
AuthController.php:
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (! $token = $this->guard()->attempt($credentials)) {
return response()->json(['error' => 'Email or Password invalid'], 401);
}
return $this->respondWithToken($token);
}
Inside User Model:
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
config/auth.php:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
router/api.php:
Route::group([
'middleware' => 'api',
'namespace' => 'App\Http\Controllers',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
uj5u.com熱心網友回復:
我不得不在密碼欄位中使用 bcrypt 格式而不是 md5。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/438755.html
上一篇:如何將{poliished}包與{brochure}框架一起使用?
下一篇:kubectl版本錯誤:exec插件配置為使用API版本client.authentication.k8s.io/v1alpha1
