您的客户端是否能够在标头中发送访问令牌?
Authorization: Bearer <token>
如果是这样,您可以使用代码中已有的auth:api,在这里您可以通过使用 Guzzle 的示例来了解如何调用它:
https://laravel.com/docs/5.6/passport#protecting-routes
$response = $client->request('GET', '/api/user', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
],
]);
否则,与我的建议相反,因为使用 heather 授权更加标准和安全,您可能有两个(不是很合适的)替代方案:
- 您可以创建一个中间件来检查石南花中是否没有授权,但在正文中,如果有,请将其移至
auth:api中间件之前的石南花(但请务必先运行此中间件)。
- 删除
auth:api 中间件并在创建您自己的中间件或在控制器本身中进行身份验证。
关于 Laravel 中间件的文档:
https://laravel.com/docs/5.6/middleware
您可以在此处找到有关 Laravel 开箱即用身份验证的更多信息:
https://laravel.com/docs/5.6/authentication
注意:确保文档版本和你的 Laravel 版本匹配。
有关 Barer 身份验证的更多信息:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
这是中间件如何工作的示例(未经测试):
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class BodyAuthenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (!Auth::guard($guard)->check()
&& null !== ($token = $request->json('access.user.accessToken', null))) {
$request->headers->add([
'Authorization' => 'Bearer ' . $token,
]);
}
return $next($request);
}
}
您还可以在此处查看 Passport 中间件代码:
https://github.com/laravel/passport/blob/5.0/src/Http/Middleware/CreateFreshApiToken.php
您有不同的方式来注册您的中间件:
https://laravel.com/docs/5.6/middleware#registering-middleware
所以你必须编辑这个文件:
https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php
根据您的 API 需求,您可以执行以下操作:
protected $routeMiddleware = [
...
'auth.body' => \App\Http\Middleware\BodyAuthenticate::class,
];
然后你可以在你的路由中添加这个中间件:
Route::post('/client', function (Request $request) {
$data = $request->json()->all();
return $data;
})->middleware('auth.body', 'auth:api');
或者做一些更全局的东西(如果所有 API 调用都需要令牌认证)将中间件添加到 api 中间件组(也在 App\Http\Kernel 类中):
'api' => [
'throttle:60,1',
'bindings',
'auth.body',
'auth:api',
],
然后,如果发送的令牌与数据库中的任何令牌匹配,则身份验证单例将返回拥有它的用户。您可以像这样获得该用户:
https://laravel.com/docs/5.6/authentication#retrieving-the-authenticated-user
use Illuminate\Support\Facades\Auth;
// Get the currently authenticated user...
$user = Auth::user();
// Get the currently authenticated user's ID...
$id = Auth::id();
请记住,客户端必须在每次调用(不是会话)中发送令牌。
这样你就可以保护路线了:
- 按照文档的建议(在路由或控制器中):
https://laravel.com/docs/5.6/authentication#protecting-routes
Route::get('client', function () {
// Only authenticated users may enter...
})->middleware('auth.body', 'auth:api');
或者在控制器中:
public function __construct()
{
$this->middleware('auth.body', 'auth:api');
}
- 团体路线:
Route::middleware(['auth.body', 'auth:api'])->group(function () {
Route::get('client', function () {
// Uses first & second Middleware
});
Route::post('client', function (Request $request) {
// Uses first & second Middleware
$data = $request->json()->all();
return $data;
});
Route::get('client/user/profile', function () {
// Uses first & second Middleware
});
});
- 如果您编辑
App\Http\Kernel 以全局添加中间件(您不需要组):
Route::get('client', function () {
// Uses first & second Middleware
});
Route::post('client', function (Request $request) {
// Uses first & second Middleware
$data = $request->json()->all();
return $data;
});
Route::get('client/user/profile', function () {
// Uses first & second Middleware
});
提示:您可以使用组来添加,不仅是middleware,还可以添加其他有趣的参数,例如控制器namespace、domain、使用as 命名别名前缀或URI path 前缀。
例子:
Route::group([
'namespace' => 'Client', // Loads from App\Http\Controllers\Client
'domain' => 'client.domain.com',
'as' => 'client::', // Check with `php artisan route:list --name=client`
'middleware' => ['auth.body', 'auth:api'],
'prefix' => 'api',
], function () {
// Uses first & second Middleware
// GET https://client.domain.com/api/
Route::get('/', function () {
// ...
});
// Uses first & second Middleware
// GET https://client.domain.com/api/profile
Route::get('client/profile', function () {
$user = Auth::user();
// ...
});
// Uses first & second Middleware
// POST https://client.domain.com/api/profile
Route::post('client/profile', function (Request $request) {
// ...
});
// Uses first & second Middleware
// App\Http\Controllers\Client\PhotoController
// @link: https://laravel.com/docs/5.6/controllers#resource-controllers
// GET /photos index photos.index
// GET /photos/create create photos.create
// POST /photos store photos.store
// GET /photos/{photo} show photos.show
// GET /photos/{photo}/edit edit photos.edit
// PUT/PATCH /photos/{photo} update photos.update
// DELETE /photos/{photo} destroy photos.destroy
Route::resource('photos', 'PhotoController');
//...
});
请注意,如果您编辑 App\Http\Kernel 以全局添加中间件,则不需要组数组中的 middleware。