Php 如何在Lumen中使用外部身份验证API对令牌进行身份验证

Php 如何在Lumen中使用外部身份验证API对令牌进行身份验证,php,microservices,lumen,Php,Microservices,Lumen,我正在开发流明的微型服务。微服务将使用Auth microservice生成API密钥或ClientID/Secret(JWT令牌) 每当具有有效的令牌或API密钥的客户发送API调用时,我想使用Auth Microservice对其进行验证 为了验证,我创建了 /api/oauth/verify - with encrypted TOKEN in header in Auth Microservice 此端点将在验证时返回用户的id 下面是我在微服务中间件上发送令牌进行验证的代码

我正在开发流明的微型服务。微服务将使用Auth microservice生成
API密钥
ClientID/Secret(JWT令牌)

每当具有有效的
令牌
API密钥
的客户发送API调用时,我想使用Auth Microservice对其进行验证

为了验证,我创建了

/api/oauth/verify - with encrypted TOKEN in header in Auth Microservice
此端点将在验证时返回用户的
id

下面是我在微服务中间件上发送令牌进行验证的代码

        $newEncrypter = new \Illuminate\Encryption\Encrypter( config('app.key'), Config::get( 'app.cipher' ) );
        $encryptedBearerToken = $newEncrypter->encrypt( $request->bearerToken() );
        //dd($bearerToken);
        $client = new Client(
            [
                'headers' => [
                    'Accept' => 'application/json',
                    'Authorization' => 'Bearer ' . $request->bearerToken()
                ],
                'verify' => false
            ]);
        $response = $client->post($url, ['json' => ['token' => $encryptedBearerToken]])->getBody()->getContents();
        $response = json_decode($response);
        $request->user = $response;
Auth微服务中间件代码

    $bearerToken = $request->bearerToken();
    $encryptedToken = $request->token;
    if(!empty($encryptedToken))
    {
        $newEncrypter = new \Illuminate\Encryption\Encrypter( config('app.message_key'), Config::get( 'app.cipher' ) );
        $decryptedBearerToken = $newEncrypter->decrypt( $encryptedToken );

        if($bearerToken !== $decryptedBearerToken)
        {
            return $this->invalidToken();
        }
    }

    $validateApi = $this->authenticate(['api']);
    if(!$validateApi)
    {
        if(empty($bearerToken))
        {
            return $this->invalidToken();
        }
        $user = User::where('api_key', $bearerToken)->first();

        if(empty($user))
        {
            return $this->invalidToken();
        }
        \Illuminate\Support\Facades\Auth::setUser($user);
    }
关于上述代码实现的一些问题

  • 向外部API发送加密令牌的方法有效吗
  • 这是验证令牌或API密钥的最佳方法吗
问题:

  • 中间件上验证微服务的Curl请求非常耗时,负载测试导致
    502网关超时
如何以更好的方式对令牌进行身份验证并优化身份验证

[注意:执行单个请求时,实现工作正常 [经邮递员]