*/ class ImplicitController extends Controller { /** * @throws \Exception */ public function init() { parent::init(); $grant = new ImplicitGrant(new \DateInterval(Yii::$app->params['user.accessTokenExpire'])); Yii::$app->services->oauth2Server->set($grant); // 写入服务 } /** * @throws NotFoundHttpException */ public function actionIndex() { /* @var \League\OAuth2\Server\AuthorizationServer $server */ $server = Yii::$app->services->oauth2Server->get(); $response = new Response(); $request = ServerRequest::fromGlobals(); // Try to respond to the request try { // 验证HTTP请求并返回 $authRequest = $server->validateAuthorizationRequest($request); // 是否授权成功 $authRequest->setAuthorizationApproved(true); $authRequest->setUser(new UserEntity()); $server->completeAuthorizationRequest($authRequest, $response); } catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) { throw new NotFoundHttpException($exception->getMessage()); } catch (\Exception $exception) { throw new NotFoundHttpException($exception->getMessage()); } } }