我已经为我的网络应用程序创建了一个 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/