为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
米琪卡哇伊 2023-10-01 17:09:08
所以我将从问题开始。我有与不记名令牌身份验证一起使用的前端应用程序,该应用程序发送到我的后端。身份验证的一切都很好,直到我想从我的路线获取用户数据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 文档中的每一步操作,但根本不起作用。
查看完整描述

1 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

我找到了问题的解决方案。这是我的错误,因为我的User.php模型中有拼写错误


变更前型号:


<?php


namespace App;


use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Tymon\JWTAuth\Contracts\JWTSubject;


class User extends Authenticatable implements JWTSubject

{

    use Notifiable;


    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'password',

        'verified', 'balance', 'isAdmin',

    ];


    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'verified',

    ];


    /**

     * getJWTCustomClaims

     *

     * @return mixed

     */

    public function getJWTCustomClaims()

    {

        return [];

    }


    /**

     * getJWTIdentifier

     *

     * @return array

     */

    public function getJWTIdentifier()

    {

        return $this->key;

    }

    

    /**

     * RelationShip between user, and user activation token

     *

     * @return void

     */

    public function verifyToken()

    {

        return $this->hasOne(UserVerification::class);

    }

}

变更后型号:


<?php


namespace App;


use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Tymon\JWTAuth\Contracts\JWTSubject;


class User extends Authenticatable implements JWTSubject

{

    use Notifiable;


    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'password',

        'verified', 'balance', 'isAdmin',

    ];


    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'verified',

    ];


    /**

     * getJWTCustomClaims

     *

     * @return mixed

     */

    public function getJWTCustomClaims()

    {

        return [];

    }


    /**

     * getJWTIdentifier

     *

     * @return array

     */

    public function getJWTIdentifier()

    {

        return $this->getKey();

    }

    

    /**

     * RelationShip between user, and user activation token

     *

     * @return void

     */

    public function verifyToken()

    {

        return $this->hasOne(UserVerification::class);

    }

}

所以我将getJWTIdentifier()返回从更改$this->key为$this->getKey()


查看完整回答
反对 回复 2023-10-01
  • 1 回答
  • 0 关注
  • 60 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信