Php laravel使用承载令牌从控制器消耗自己的api
我喜欢在Laravel中通过api执行crud操作。因此,移动应用程序和web应用程序都可以使用相同的api端进行crud操作。出于api安全目的,我使用passport。在使用postman调用api end时我成功了,而在尝试从laravel控制器调用api end时,我得到了空输出。我正在尝试使用不记名代币 在前面,我有laravel刀片视图。我在laravel中安装了passport。我在api.php中创建了几个api端点 其中一个是启用了auth:api中间件的Php laravel使用承载令牌从控制器消耗自己的api,php,laravel,api,laravel-passport,Php,Laravel,Api,Laravel Passport,我喜欢在Laravel中通过api执行crud操作。因此,移动应用程序和web应用程序都可以使用相同的api端进行crud操作。出于api安全目的,我使用passport。在使用postman调用api end时我成功了,而在尝试从laravel控制器调用api end时,我得到了空输出。我正在尝试使用不记名代币 在前面,我有laravel刀片视图。我在laravel中安装了passport。我在api.php中创建了几个api端点 其中一个是启用了auth:api中间件的/user/{id}
/user/{id}
我有web.php
Route::get('profile', 'UserController@profile')->middleware('auth');
所以我的想法是从profile controller发送一个请求到/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);
}
}
$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());
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
});
这是给我的不记名代币。我正在传递这个承载令牌,并试图使用来自laravel profile controller的自己的api
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);
}
}
$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());
Route::get('/user/{id}', function ($id) {
return new UserResource(User::find($id)->customer);
});
输出为
{"profile":null}
而状态代码
是302
当我通过邮递员打电话时http://localhost:8000/api/user/1
将授权承载令牌值与之前相同
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'),它将从我的控制器中工作
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);
}
}
$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());
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());