php – Laravel Passport通过访问令牌获取客户端ID

前端之家收集整理的这篇文章主要介绍了php – Laravel Passport通过访问令牌获取客户端ID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在写一个小sms网关,供几个项目使用,

我实施了laravel护照认证(client credentials grant token)

然后我将CheckClientCredentials添加到api中间件组:

protected $middlewareGroups = [
    'web' => [
       ...
    ],'api' => [
        'throttle:60,1','bindings',\Laravel\Passport\Http\Middleware\CheckClientCredentials::class
    ],];

逻辑工作正常,现在在我的控制器中我需要让客户端与有效令牌相关联.

routes.PHP文件

Route::post('/sms',function(Request $request) {
    // save the sms along with the client id and send it

    $client_id = ''; // get the client id somehow

    sendSms($request->text,$request->to,$client_id);
});

出于明显的安全原因,我永远不能使用消费者请求发送客户端ID,例如$client_id = $request-> client_id;.

有一种棘手的方法.
您可以在中间件CheckClientCredentials中修改句柄方法,只需添加此行即可.
$request["oauth_client_id"] = $psr->getAttribute('oauth_client_id');

然后你可以在controller的函数中得到client_id:

public function info(\Illuminate\Http\Request $request)
{
    var_dump($request->oauth_client_id);
}

猜你在找的Laravel相关文章