php bearer,php – 自定义Laravel Passport BearerTokenResponse

目前我使用Laravel Passport进行Laravel安装(使用联盟/ oauth2服务器进行服务器实现).我想在授予oauth2令牌时返回用户ID,因此我可以使用它来识别我的EmberJS应用程序中经过身份验证的用户.

建议的方法是:

创建我自己的类:

use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;

class UserIdBearerTokenResponse extends BearerTokenResponse

{

protected function getExtraParams(AccessTokenEntityInterface $accessToken)

{

return [

'user_id' => $this->accessToken->getUserIdentifier()

];

}

}

修改vendor / league / oauth2-server / src中的AuthorizationServer.getResponseType()

protected function getResponseType()

{

if ($this->responseType instanceof ResponseTypeInterface === false) {

// Return my own class instead of provided one

$this->responseType = new UserIdBearerTokenResponse();

}

$this->responseType->setPrivateKey($this->privateKey);

return $this->responseType;

}

但是这种方法要求我将vendor / league / oauth2-server / src / AuthorizationServer.php文件添加到我的git repo中.

这对我来说似乎非常混乱和不可靠.是否有更好/更清洁的方法来实现这一目标?

解决方法:

要使用自定义响应,您可以添加如下自定义授权服务器:

namespace App;

use League\OAuth2\Server\AuthorizationServer;

use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;

class TokenServer extends AuthorizationServer

{

/**

* Get the token type that grants will return in the HTTP response.

*

* @return ResponseTypeInterface

*/

protected function getResponseType()

{

if ($this->responseType instanceof ResponseTypeInterface === false) {

$this->responseType = new UserIdBearerTokenResponse();

}

$this->responseType->setPrivateKey($this->privateKey);

return $this->responseType;

}

}

和这样的自定义PassportServiceProvider:

namespace App\Providers;

use App\TokenServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider

{

/**

* Make the authorization service instance.

*

* @return AuthorizationServer

*/

public function makeAuthorizationServer()

{

return new TokenServer(

$this->app->make(\Laravel\Passport\Bridge\ClientRepository::class),

$this->app->make(\Laravel\Passport\Bridge\AccessTokenRepository::class),

$this->app->make(\Laravel\Passport\Bridge\ScopeRepository::class),

'file://'.storage_path('oauth-private.key'),

'file://'.storage_path('oauth-public.key')

);

}

}

然后在config / app.php文件中进行以下更改:

/*

* Package Service Providers...

* We extend the packaged PassportServiceProvider with our own customization

*/

// Laravel\Passport\PassportServiceProvider::class,

App\Providers\PassportServiceProvider::class,

标签:php,laravel,laravel-passport

来源: https://codeday.me/bug/20191005/1856621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值