我想附加Authorization: Bearer {yourtokenhere}
在我的 Laravel 中。如果在 Postman 中,我将 Authorization 放在 Headers 选项卡上并通过编写手动给出令牌值,Bearer {token}
这样我就可以保护特定的路线,但是如何在 Laravel 源代码中做到这一点?我应该在我的控制器中做什么,或者我应该在中间件或内核或其他地方添加另一种方法?这是因为我得到了{"error":"token_not_provided"}
每个访问受保护的路由jwt.auth middleware
。
这是给我的受保护路线{"error":"token_not_provided"}
:
Route::group(['middleware' => ['jwt.auth']], function(){
Route::get('/dashboard', 'AppController@dashboard');
});
这是我在 AuthController 中的登录方法:
public function signin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);
// grab credentials from the request
$credentials = $request->only('username', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json([
'error' => 'Invalid Credentials, username and password dismatches. Or username may not registered.',
'status' => '401'
], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json([
'user_id' => $request->user()->id,
'token' => $token
]);
}