我目前无法使用passport middleware保护我的路线。我正在使用lumen-passport,这是一个让Passport与Lumen兼容的软件包。当我尝试按以下路线时:
$app->get('/test', 'Game@test')->middleware('auth:api');
我得到以下内容:
FatalThrowableError in AuthManager.php line 294:
Call to undefined method Illuminate\Auth\RequestGuard::handle()
in AuthManager.php line 294
at AuthManager->__call('handle', array(object(Request), object(Closure), 'api')) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in RoutesRequests.php line 780
at Application->sendThroughPipeline(array('auth:api'), object(Closure)) in RoutesRequests.php line 534
at Application->dispatch(null) in RoutesRequests.php line 475
at Application->run() in index.php line 28
我的auth.php
如果有帮助:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \App\User::class
]
],
'passwords' => [
//
],
路由是正确的,因为删除中间件会按预期重定向到适当的控制器。任何帮助将不胜感激。
答案 0 :(得分:0)
我不知道你是否解决了你的问题,但我有同样的问题,我解决了在Controller的构造函数中添加中间件'auth:api',在我的例子中UserController:
public function __construct(){
$this->middleware('auth:api');
}
答案 1 :(得分:0)
对我来说,问题原来是我的路线格式错误。所以要调试我注释掉了所有现有路由并添加了代码(确保在.env文件中设置了APP_KEY = xxx):
$app->group(['middleware' => 'auth:api'], function($app) {
$app->get('/user/info', function() {
$user = app('auth')->guard()->user();
return response()->json($user);
});
});
路线/用户/信息没有错误。
然后用以下格式用现有路线替换我的路线:
$app->group(['middleware' => 'auth:api'], function($app)
{
$app->get('/api/v1/users', [
'as' => 'listall', 'uses' => 'MyController@listModels'
]);
$app->get('/api/v1/users/{id}', [
'as' => 'list', 'uses' => 'MyController@listModel'
]);
$app->post('/api/v1/users', [
'as' => 'create', 'uses' => 'MyController@createModel'
]);
$app->delete('/api/v1/users/{id}', [
'as' => 'delete', 'uses' => 'MyController@deleteModel'
]);
$app->put('/api/v1/users/{id}', [
'as' => 'update', 'uses' => 'MyController@updateModel'
]);
});
错误不再被抛出。
答案 2 :(得分:0)
您必须在Authenticate.php中添加此代码
<div class="parent">
<div class="test">
<div>The first paragraph.</div>
</div>
<div class="test">The second paragraph.</div>
<div class="test">The third paragraph.</div>
<div class="test">The fourth paragraph.</div>
<div class="test-1">The firth paragraph.</div>
</div>
答案 3 :(得分:-1)
您所要做的就是位于bootstrap文件夹中的app.php。
你应该定义护照中间件。
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'client_credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class
]);
完成后,您必须将受保护的URL设置为client_credentials中间件。
完成后尝试使用令牌向受保护路由发出请求,例如返回Auth :: id()。
您将收到此令牌的用户ID。
问候