gpt4 book ai didi

php - 如何在 Laravel/Passport 中重写 `issueToken` 方法

转载 作者:行者123 更新时间:2023-12-03 08:56:19 24 4
gpt4 key购买 nike

我试图覆盖位于 \Laravel\Passport\Http\Controllers\AccessTokenController 中的 issueToken 方法,而不添加新路由,即(api/oauth/token )。我已经构建了自己的 AccessTokenController 来执行我想要的功能,但是当我调用 oauth/token 时,正在执行父方法而不是我的实现。我是否遗漏了一些东西来完成覆盖?

<?php
namespace App\Http\Controllers;

use phpseclib\Crypt\RSA;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ServerRequestInterface;
use \Laravel\Passport\Http\Controllers\AccessTokenController as ATC;

class AccessTokenController extends ATC {

public function issueToken(ServerRequestInterface $request){
try {
// Fetching username from request
$username = $request->getParseBody()['username'];
// Fetching the User
$user = User::where('email', '=', $username);
// Genereting token
$tokenResponse = parent::issueToken($request);
//convert response to json string
$content = $tokenResponse->getBody()->__toString();
$content['email'] = $user['emial'];
//convert json to array
$data = json_decode($content, true);
return $data;

} catch (OAuthServerException $e) {
return ['Message' => 'The suer credentials were incorrect!'];
}catch (\Exception $e) {
return ['Message' => 'Exception: '.$e];
}
}
}

最佳答案

只需覆盖 app/Providers/AuthServiceProvider.php 中的路由就足够了。

确保在文件顶部添加:use Illuminate\Support\Facades\Route;!

public function boot()
{
$this->registerPolicies();

Passport::routes();

Route::post('/oauth/token', [
'uses' => '\App\Http\Controllers\AccessTokenController@issueToken',
'as' => 'passport.token',
'middleware' => 'throttle',
]);
}

php artisan route:list --name=passport.token 的输出:

+--------+----------+-------------------------+-------------------------+---------------------------------------------------------------------------+------------+| Domain | Method   | URI                     | Name                    | Action                                                                    | Middleware |+--------+----------+-------------------------+-------------------------+---------------------------------------------------------------------------+------------+|        | POST     | oauth/token             | passport.token          | App\Http\Controllers\AccessTokenController@issueToken                     | throttle   ||        | POST     | oauth/token/refresh     | passport.token.refresh  | Laravel\Passport\Http\Controllers\TransientTokenController@refresh        | web,auth   ||        | GET|HEAD | oauth/tokens            | passport.tokens.index   | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | web,auth   ||        | DELETE   | oauth/tokens/{token_id} | passport.tokens.destroy | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | web,auth   |+--------+----------+-------------------------+-------------------------+---------------------------------------------------------------------------+------------+

关于php - 如何在 Laravel/Passport 中重写 `issueToken` 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55054182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com