(API) Laravel 7 上不允许使用 tymon/jwt-auth 的 405 方法
Posted
技术标签:
【中文标题】(API) Laravel 7 上不允许使用 tymon/jwt-auth 的 405 方法【英文标题】:(API) 405 Method Not Allowed on Laravel 7 with tymon/jwt-auth 【发布时间】:2020-12-02 23:36:49 【问题描述】:所以我要从这个问题开始。
我的前端应用程序与承载令牌身份验证一起使用,它被发送到我的后端。
在我想从我的路由中获取我的用户数据之前,一切都可以进行身份验证
Route::get('api/auth/me','Backend\AuthController@me');
我收到错误 405 Method GET
not allowed
完整的错误信息:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\Programming\LSUniverseCMS\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117
我已经设置了回复GET
的路线
这是我的api.php
文件:
<?php
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'auth', 'middleware' => 'api'], function ($router)
Route::post('login', 'Backend\AuthController@login')->name('login');
Route::post('register', 'Backend\AuthController@register')->name('register');
Route::post('refresh', 'Backend\AuthController@refresh')->name('refresh');
Route::post('logout', 'Backend\AuthController@logout')->name('logout');
Route::get('verify/token', 'Backend\VerificationController@verify')->name('verify');
Route::get('me', 'Backend\AuthController@me')->name('me');
);
Route::group(['middleware' => 'api', 'prefix' => 'user'], function ($router)
);
我的AuthController.php
文件:
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\User;
use App\UserVerification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class AuthController extends Controller
/**
* Create instance of AuthController
* Make middleware ignore login and register routes
*
* @return void
*/
public function __construct()
$this->middleware('auth:api', ['except' =>['login','register']]);
/**
* Register the user with requested credentials
*
* @param mixed $request
* @return void
*/
public function register(Request $request)
$validator = Validator::make($request->all(), [
'name' => ['required', 'min:4'],
'email' => ['email', 'required', 'unique:users'],
'password' => ['required', 'regex:/^(?=.8,)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/'],
're_password' => ['required', 'same:password'],
]);
if ($validator->fails())
return response()->json(['error' => $validator->errors()->first()], 400);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'isAdmin' => 0,
'balance' => 0.00,
'verified' => 0,
]);
if ($user)
UserVerification::create([
'user_id' => $user->id,
'token' => md5("$user->id $user->email" . sha1(time())),
]);
return response()->json(['message' => 'Created'], 201);
return response()->json(['error' => 'Failed'], 400);
/**
* Get JWT token via given credentials
*
* @return mixed
*/
public function login(Request $request)
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);
$credentials = request(['email', 'password']);
$verified = User::where('email', $credentials['email'])->first()->verified;
if ($verified == 1)
if (!$token = auth('api')->attempt($credentials))
return response()->json(['error' => 'Unauthorized'], 401);
else
return response()->json(['error'=>'Not verified'], 401);
return $this->respondWithToken($token);
/**
* Return user information from database
*
* @return void
*/
public function me()
return response()->json(auth('api')->user());
/**
* Log the user out (Invalidate the token)
*
* @return \Illuminate\Http\Response;
*/
public function logout()
auth()->logout();
return response()->json(['message' => 'Successfuly']);
/**
* Refresh user token
*
* @return void
*/
public function refresh()
return $this->respondWithToken(auth()->refresh());
/**
* respondWithToken
*
* @param mixed $token
* @return void
*/
protected function respondWithToken($token)
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,
]);
我的路线:列表:
+--------+----------+-------------------------+----------+------------------------------------------------------------+------------+
| 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 |
| | POST | api/auth/register | register | App\Http\Controllers\Backend\AuthController@register | api |
| | GET|HEAD | api/auth/verify/token | verify | App\Http\Controllers\Backend\VerificationController@verify | api |
| | GET|HEAD | path? | | Illuminate\Routing\ViewController | web |
+--------+----------+-------------------------+----------+------------------------------------------------------------+------------+
我的 URI 标签: https://i.imgur.com/cT1MMS7.png
那么我的错误在哪里?请帮助我,我可能坚持了 3 个多小时 我遵循了 tymondesign/jwt-auth 文档中的每一步,但根本没有工作。
【问题讨论】:
检查网络选项卡中的 php artisan route:list 和 uri 以检查它们是否匹配。最后清除缓存 php artisan optimize:clear 我已经试过了,还是不行 好的,你能在网络标签中分享路由:列表和uri吗? 在引导程序中 -> 缓存删除除 .gitignore 之外的所有文件,然后在运行 composer update 后运行 composer dump-autoload @TEFO 已经尝试过了。 【参考方案1】:我找到了问题的解决方案。
这是我的错误,因为我的 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()
return 从$this->key
更改为$this->getKey()
【讨论】:
以上是关于(API) Laravel 7 上不允许使用 tymon/jwt-auth 的 405 方法的主要内容,如果未能解决你的问题,请参考以下文章