laravel 拥有 api 使用不记名令牌的控制器消费
laravel own api consumption from controller with bearer token
我喜欢在Laravel中通过api做crud操作。因此移动应用程序/Web 应用程序都可以达到相同的 api 结束以达到 crud operation.For api 安全目的我正在使用护照。我在使用邮递员调用 api end 时成功了,而我在尝试从 laravel 控制器调用 api end 时得到空输出。我正在尝试使用不记名令牌。
前面我有laravelbladeviews.I已经在laravel内安装了passport。我在 api.php 中创建了几个 api 端点
其中之一是 /user/{id}
,它启用了 auth:api 中间件。
我有 web.php 有
Route::get('profile', 'UserController@profile')->middleware('auth');
所以我的想法是从配置文件控制器向 /api/user/1 发送一个带有不记名令牌
的请求
为了获取不记名令牌,我使用从登录控制器请求创建/oauth/token
if (Auth::attempt(['email' => request('email'), 'password' =>
request('password')])) {
$user = Auth::user();
if(Auth::check()) {
$req = Request::create('/oauth/token', 'POST',[
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'WDv6wrY9Tf9HuixaiQjDWGy7yBqEG1IrmsLucAUI',
'username' => request('email'),
'password' => request('password'),
'scope' => '*'
]);
$res = app()->handle($req);
$responseBody = json_decode($res->getContent()); // convert to json object
return response()->json(['success' => $responseBody], $res->getStatusCode());
} else {
return response()->json(['error' => 'Not logged in '], 401);
}
}
它 return 我的不记名令牌。我正在传递此不记名令牌并尝试从 laravel 配置文件控制器
使用自己的 api
$token ='eyJ0eXAiOiJKV1QiLCJhbGciOi...';
$req = Request::create('/api/user/1', 'GET',[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
$res = app()->handle($req);
$profile_details = json_decode($res->getContent()); // convert to json object
return response()->json(['profile' =>$profile_details], $res->getStatusCode());
输出为
{"profile":null}
和status code
是302
当我通过post man
打电话时http://localhost:8000/api/user/1
将 Authorization Bearer Token 值与之前相同
eyJ0eXAiOiJKV1QiLCJhbGciOi.....
我收到以下回复
{
"data": {
"id": 1,
"first_name": "rajib",
"last_name": "chow",
"address": "207 Eryn Coves",
"city": "South Treva",
"country": "Luxembourg",
"phone": "(397) 400-2302 x6997",
"fax": "82928",
"user_id": 1,
"created_at": "2019-06-26 15:03:31",
"updated_at": "2019-06-26 15:03:31"
}
}
我的 api 路线是
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
})->middleware('auth:api');
如果我再次从代码中删除中间件 ('auth:api'),它将在我的控制器中工作
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
});
上面给我预期的输出
但出于安全考虑,我认为我应该在调用 api 时传递不记名令牌。
我怎样才能从控制器发出 api 呼叫以及不记名令牌
尝试使用此代码
$request = Request::create('/api/user/1', 'GET');
$request->headers->set('Accept', 'application/json');
$request->headers->set('Authorization', 'Bearer '.$token);
$res = app()->handle($request);
$profile_details = json_decode($res->getContent()); // convert to json object
return response()->json(['profile' =>$profile_details], $res->getStatusCode());
我喜欢在Laravel中通过api做crud操作。因此移动应用程序/Web 应用程序都可以达到相同的 api 结束以达到 crud operation.For api 安全目的我正在使用护照。我在使用邮递员调用 api end 时成功了,而我在尝试从 laravel 控制器调用 api end 时得到空输出。我正在尝试使用不记名令牌。
前面我有laravelbladeviews.I已经在laravel内安装了passport。我在 api.php 中创建了几个 api 端点
其中之一是 /user/{id}
,它启用了 auth:api 中间件。
我有 web.php 有
Route::get('profile', 'UserController@profile')->middleware('auth');
所以我的想法是从配置文件控制器向 /api/user/1 发送一个带有不记名令牌
的请求为了获取不记名令牌,我使用从登录控制器请求创建/oauth/token
if (Auth::attempt(['email' => request('email'), 'password' =>
request('password')])) {
$user = Auth::user();
if(Auth::check()) {
$req = Request::create('/oauth/token', 'POST',[
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'WDv6wrY9Tf9HuixaiQjDWGy7yBqEG1IrmsLucAUI',
'username' => request('email'),
'password' => request('password'),
'scope' => '*'
]);
$res = app()->handle($req);
$responseBody = json_decode($res->getContent()); // convert to json object
return response()->json(['success' => $responseBody], $res->getStatusCode());
} else {
return response()->json(['error' => 'Not logged in '], 401);
}
}
它 return 我的不记名令牌。我正在传递此不记名令牌并尝试从 laravel 配置文件控制器
使用自己的 api$token ='eyJ0eXAiOiJKV1QiLCJhbGciOi...';
$req = Request::create('/api/user/1', 'GET',[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
$res = app()->handle($req);
$profile_details = json_decode($res->getContent()); // convert to json object
return response()->json(['profile' =>$profile_details], $res->getStatusCode());
输出为
{"profile":null}
和status code
是302
当我通过post man
打电话时http://localhost:8000/api/user/1
将 Authorization Bearer Token 值与之前相同
eyJ0eXAiOiJKV1QiLCJhbGciOi.....
我收到以下回复
{
"data": {
"id": 1,
"first_name": "rajib",
"last_name": "chow",
"address": "207 Eryn Coves",
"city": "South Treva",
"country": "Luxembourg",
"phone": "(397) 400-2302 x6997",
"fax": "82928",
"user_id": 1,
"created_at": "2019-06-26 15:03:31",
"updated_at": "2019-06-26 15:03:31"
}
}
我的 api 路线是
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
})->middleware('auth:api');
如果我再次从代码中删除中间件 ('auth:api'),它将在我的控制器中工作
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
});
上面给我预期的输出
但出于安全考虑,我认为我应该在调用 api 时传递不记名令牌。 我怎样才能从控制器发出 api 呼叫以及不记名令牌
尝试使用此代码
$request = Request::create('/api/user/1', 'GET');
$request->headers->set('Accept', 'application/json');
$request->headers->set('Authorization', 'Bearer '.$token);
$res = app()->handle($request);
$profile_details = json_decode($res->getContent()); // convert to json object
return response()->json(['profile' =>$profile_details], $res->getStatusCode());