[英]Laravel Dingo Authentication JWT Required on Internal Requests
I'm using Laravel Dingo to consume API requests (internally) within Laravel controllers, and have come up against a problem with routes which are authenticated with tokens in headers. 我正在使用Laravel Dingo在Laravel控制器内(内部)使用API请求,并且遇到了使用标头中的令牌进行身份验证的路由的问题。 I'm getting an Exception thrown, looking for a token in requests. 我抛出异常,在请求中寻找令牌。
This is my API routes: 这是我的API路线:
$api = app('Dingo\Api\Routing\Router');
$api->version(['v1'], function ($api) {
$api->post('/users/addEmployee', "App\Http\Controllers\Api\UserController@storeEmployee")->middleware('jwt.auth');
}
Note the middleware added to the call. 注意添加到呼叫中的中间件。 When using Postman, this works 100% and adds a new employee. 使用Postman时,此方法可以100%工作并添加新员工。
When calling it internal with Dingo, I get back the typical 'token not found' error that I would typically see from the API when the Authorisation Header is missing. 当在Dingo内部调用它时,我会得到典型的“未找到令牌”错误,该错误通常是在缺少授权标头时从API中看到的。
try{
$dispatcher = app('Dingo\Api\Dispatcher');
$payload = [
'name' => $request->name,
'email' => $request->email,
];
$registerResponse = $dispatcher->be(Auth::user())->with($payload)->post('/api/users/addEmployee');
catch( InternalHttpException $internal ){
echo($internal->getResponse());die();
return Redirect::back()->withInput()->withErrors($v->getErrors());
}
My question is this, do I need to add the token, or is there a way to 'turn off' jwt middleware for an internal request? 我的问题是,我是否需要添加令牌,或者是否有办法针对内部请求“关闭” jwt中间件? Does the ->be method not handle all of this within Dingo? -> be方法不能在Dingo中处理所有这些吗? ps Auth::user is not null, I've checked that. ps Auth :: user不为null,我已经检查过了。
Solved it, you can manually set headers on outbound requests prior to it being dispatched. 解决该问题后,您可以在发送请求之前在出站请求上手动设置标头。 Didn't see the method anywhere in the docs, but following works. 在文档中的任何地方都没有看到该方法,但是可以正常工作。
$token = JWTAuth::fromUser(Auth::user());
$registerResponse = $this->api->header('Authorization','Bearer:'.$token)->with($payload)->be(Auth::user())->post('/api/users/addEmployee');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.