bootstrap / app.php(重要位)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
$app->register(App\Providers\AuthServiceProvider::class);
$app->configure('auth');
配置/ auth.php
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'token' => [
'driver' => 'token',
'provider' => 'users',
],
],
AuthServiceProvider.php
public function boot()
{
$this->app['auth']->viaRequest('api', function ($request) {
return User::first();
//if ($request->input('api_token')) {
//return User::where('api_token', $request->input('api_token'))->first();
//}
});
}
中间件/ Authenticate.php
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
路线:
Get request to /api/v1/auth protected by 'auth:api' middleware with
Authorization header
Bearer eyJpdiI6IklONFQrc1Y2dEF3MHJTOU1QWWVndWc9PSIsInZhbHVlIjoiZ1h6...
使用createToken函数生成Bearer令牌并正确传递。然而,我总是收到未经授权的回复。
使用流明和流明护照包。