*/ class JWTAuth extends Behavior { /** * @var array */ public $optional = []; /** * @return array */ public function events() { return [Controller::EVENT_BEFORE_ACTION => 'beforeAction']; } /** * @param $event * @return bool * @throws UnauthorizedHttpException * @throws UnprocessableEntityHttpException */ public function beforeAction($event) { if (in_array(Yii::$app->controller->action->id, $this->optional)) { return true; } $accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface $publicKeyPath = 'file://' . Yii::getAlias(Yii::$app->services->config->backendConfig('oauth2_rsa_public')); $server = new \League\OAuth2\Server\ResourceServer( $accessTokenRepository, new CryptKey($publicKeyPath, null, !StringHelper::isWindowsOS()) ); try { // 手动写入 header // Yii::$app->params['Authorization'] = 'Bearer ' . $token; $request = ServerRequest::fromGlobals(); $server->validateAuthenticatedRequest($request); } catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) { throw new UnauthorizedHttpException($exception->getMessage()); } catch (\Exception $exception) { throw new UnprocessableEntityHttpException($exception->getMessage()); } $user = $request->getAttributes(); /** @var IdentityInterface $user */ if ($user = Yii::$app->services->oauth2AccessToken->findByAccessToken($user['oauth_access_token_id'], $user['oauth_client_id'])) { Yii::$app->user->login($user); } else { throw new UnauthorizedHttpException('用户不存在'); } return true; } }