加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Oauth2-server-laravel自定义消息响应

发布时间:2020-12-14 19:39:58 所属栏目:大数据 来源:网络整理
导读:我正在使用 Oauth-server-laravel身份验证. 到目前为止我做了什么: 当我将错误的access_token发布到我在laravel中创建的API时,它会给出以下响应, { "error": "access_denied","error_description": "The resource owner or authorization server denied the
我正在使用 Oauth-server-laravel身份验证.

到目前为止我做了什么:

当我将错误的access_token发布到我在laravel中创建的API时,它会给出以下响应,

{
  "error": "access_denied","error_description": "The resource owner or authorization server denied the request."
}

我在路线中使用了oauth作为中间件,如下所示,

Route::group(['namespace' => 'ModulesUserHttpControllers','middleware' => 'oauth'],function () {

    // Get User Profile Details
    Route::post('getUserProfileDetail','UserController@getUserProfileDetail');
});

问题:

If credentials are wrong then oauth automatically respond with it’s default message and i want to customize that response messages,

我有一半成功,如果凭据是正确的,那么它调用在路由中指定的函数,并且我附加我要发送的mre响应.

$response = $this->authorizer->issueAccessToken();

$code = 200; //Response OK
$response['result'] = 'success';
$response['user_id'] = $user['id'];
$response['email'] = $user['email'];
$response['name'] = $user['name'];

但是当凭据不正确时我无法发送它,因为它无法调用函数并将其默认响应发送给用户.

解决方法

我将这类问题设置为自定义消息然后 this worked for me(我写的关于这篇文章的博客文章)]

首先在app / Http / Middleware中创建一个中间件.我的中间件名称是OauthExceptionMiddleware

然后打开

app/Http/kernel.php

并把这个中间件放在$middleware数组中的oauth2以前的中间件中,就像这样

protected $middleware = [
        IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,AppHttpMiddlewareOauthExceptionMiddleware::class,];

Oauth2自定义异常错误消息

<?php
/**
 * Created by PhpStorm.
 * User: kingpabel
 * Date: 3/23/16
 * Time: 4:40 PM
 */
namespace appHttpMiddleware;
use Closure;
use LeagueOAuth2ServerExceptionOAuthException;
class OauthExceptionMiddleware
{
    public function handle($request,Closure $next)
    {
        try {
            $response = $next($request);
            // Was an exception thrown? If so and available catch in our middleware
            if (isset($response->exception) && $response->exception) {
                throw $response->exception;
            }
            return $response;
        } catch (OAuthException $e) {
            $data = [
//                'error' => $e->errorType,//                'error_description' => $e->getMessage(),'error' => 'Custom Error','error_description' => 'Custom Description',];
            return Response::json($data,$e->httpStatusCode,$e->getHttpHeaders());
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读