猿问

(API) Laravel 7 上不允许使用 tymon/jwt-auth 的 405 方法

所以我将从问题开始。


我有与不记名令牌身份验证一起使用的前端应用程序,该应用程序发送到我的后端。


身份验证的一切都很好,直到我想从我的路线获取用户数据


Route::get('api/auth/me','Backend\AuthController@me');

我收到错误 405 方法GET不允许

我已经设置了回复路线GET


我的路线:列表:


+--------+----------+-------------------------+----------+------------------------------------------------------------+------------+

| Domain | Method   | URI                     | Name     | Action                                                     | Middleware |

+--------+----------+-------------------------+----------+------------------------------------------------------------+------------+

|        | POST     | api/auth/login          | login    | App\Http\Controllers\Backend\AuthController@login          | api        |

|        | POST     | api/auth/logout         | logout   | App\Http\Controllers\Backend\AuthController@logout         | api        |

|        |          |                         |          |                                                            | auth:api   |

|        | GET|HEAD | api/auth/me             | me       | App\Http\Controllers\Backend\AuthController@me             | api        |

|        |          |                         |          |                                                            | auth:api   |

|        | POST     | api/auth/refresh        | refresh  | App\Http\Controllers\Backend\AuthController@refresh        | api        |

|        |          |                         |          |                                                            | auth:api   |

+--------+----------+-------------------------+----------+------------------------------------------------------------+------------+

我的 URI 选项卡: https://i.stack.imgur.com/aKLGu.png

那么我的错误在哪里呢?请帮助我,我可能被这个问题困扰了 3 个多小时,我按照 tymondesign/jwt-auth 文档中的每一步操作,但根本不起作用。


米琪卡哇伊
浏览 91回答 1
1回答

芜湖不芜

我找到了问题的解决方案。这是我的错误,因为我的User.php模型中有拼写错误变更前型号:<?phpnamespace App;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;use Tymon\JWTAuth\Contracts\JWTSubject;class User extends Authenticatable implements JWTSubject{&nbsp; &nbsp; use Notifiable;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that are mass assignable.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $fillable = [&nbsp; &nbsp; &nbsp; &nbsp; 'name', 'email', 'password',&nbsp; &nbsp; &nbsp; &nbsp; 'verified', 'balance', 'isAdmin',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that should be hidden for arrays.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $hidden = [&nbsp; &nbsp; &nbsp; &nbsp; 'password', 'verified',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* getJWTCustomClaims&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getJWTCustomClaims()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* getJWTIdentifier&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getJWTIdentifier()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->key;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* RelationShip between user, and user activation token&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function verifyToken()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasOne(UserVerification::class);&nbsp; &nbsp; }}变更后型号:<?phpnamespace App;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;use Tymon\JWTAuth\Contracts\JWTSubject;class User extends Authenticatable implements JWTSubject{&nbsp; &nbsp; use Notifiable;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that are mass assignable.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $fillable = [&nbsp; &nbsp; &nbsp; &nbsp; 'name', 'email', 'password',&nbsp; &nbsp; &nbsp; &nbsp; 'verified', 'balance', 'isAdmin',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that should be hidden for arrays.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $hidden = [&nbsp; &nbsp; &nbsp; &nbsp; 'password', 'verified',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* getJWTCustomClaims&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getJWTCustomClaims()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* getJWTIdentifier&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getJWTIdentifier()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->getKey();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* RelationShip between user, and user activation token&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function verifyToken()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasOne(UserVerification::class);&nbsp; &nbsp; }}所以我将getJWTIdentifier()返回从更改$this->key为$this->getKey()
随时随地看视频慕课网APP
我要回答