web-services - 在 laravel/lumen 中使用客户端 ID 和 key 访问 API

标签 web-services api laravel jwt lumen

我已经为我的网络应用程序创建了一个 API。现在我想授予对世界的访问权限,但在授予访问权限之前,我需要像 Facebook API、Twitter API、Google API 这样提供客户端 ID 和 key 的机制。目前,我正在使用 JWT AuthController,用户使用他的凭据登录并返回 token ,我不希望用户登录。

我希望用户可以使用客户端 ID 和 key 访问我的 API?另一件事是,我将如何为用户创建客户端 ID 和 key ?

这可以使用 JWT Auth 来实现吗?

有什么帮助吗?

最佳答案

我已经阅读了这篇文章,并且很有希望,但在几篇文章之后,它建议使用 oauth2,在这里:

https://laracasts.com/discuss/channels/lumen/api-authorization-via-public-and-secret-keys

引用:

Just add in the class to your API config.

namespace App\Providers\Guard;

use Dingo\Api\Auth\Provider\Authorization; use
Dingo\Api\Routing\Route; use Illuminate\Http\Request; use
Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class GuardProvider extends Authorization {
    /**
      * Get the providers authorization method.
      *
      * @return string
      */
     public function getAuthorizationMethod()
     {
         return 'X-Authorization';
     }

     /**
      * Authenticate the request and return the authenticated user instance.
      *
      * @param \Illuminate\Http\Request $request
      * @param \Dingo\Api\Routing\Route $route
      *
      * @return mixed
      */
     public function authenticate(Request $request, Route $route)
     {
         $key = $request->header(env('API_AUTH_HEADER', 'X-Authorization'));
         if (empty($key)) $key = $request->input(env('API_AUTH_HEADER', 'X-Authorization'));
         if (empty($key)) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is missing or an invalid authorization header was sent');

         $user = app('db')->select("SELECT * FROM users WHERE users.key = ?", [$key]);
         if (!$user) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is not valid');

         return $user;
     } 
}

关于web-services - 在 laravel/lumen 中使用客户端 ID 和 key 访问 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296655/

相关文章:

c# - 在哪里设置服务引用上的 CookieContainer?

php - 如何检查 URL SSL 是否有效?有没有开放的 API?

Laravel Echo 500错误未接收推送事件

laravel - 如何在 Laravel 中使用 ->append v. protected $appends

java - 在java中调用Web服务SOAP

json - 在 Sinatra 应用程序上返回 gzip 压缩的内容

c# - ASP.NET CORE 中的 CORS 策略阻止了对 XMLHttpRequest 的访问

php - 如何在中间件 Laravel 5 中捕获 "too many attempt"异常

java - 调用 webservice 时绕过 HTTPS 证书验证

api - 过滤条件的查询字符串与资源路径