【问题标题】:Laravel 5.4 mongodb passport access token UnauthenticatedLaravel 5.4 mongodb 护照访问令牌未经身份验证
【发布时间】:2017-05-17 04:27:30
【问题描述】:

我已经使用 mongodb (Jenssegers Mongodb) 构建了一个示例 Laravel 项目,我在 Laravel 5.4 中使用了护照,关注 by this document

一切正常,直到我将访问令牌交给 Postman 进行测试,现在看看我的 api.php 路由

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

在邮递员中,我设置了两个标头Accept: application/jsonAuthorization: Bearer $TOKEN,我非常确定我的访问令牌不是副本丢失错误,但仍然出现错误。

{
    "error": "Unauthenticated."
}

我尝试过的事情

我已覆盖模型User.php 中的默认id 字段

use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasApiTokens;

protected $collection = 'users';
protected $fillable = ['username', 'email', 'password', 'name'];
protected $primaryKey = '_id';

我也像这样修改AuthserviceProvider.php中的token过期时间

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

    Passport::routes();
    Passport::tokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10)
    Passport::refreshTokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10)
    Passport::pruneRevokedTokens(); //basic garbage collector

    Passport::tokensCan([
        'conference' => 'Access your conference information'
    ]);
}

还有其他一些方法,但仍然不起作用。

调试信息更新

当我将try catch 添加到public/index.php 时,出现错误

League\OAuth2\Server\Exception\OAuthServerException: The resource owner or authorization server denied the request. in /data/www/public_html/xxxx/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:165
Stack trace:
#0 /data/www/public_html/xxxx/vendor/league/oauth2-server/src/AuthorizationValidators/BearerTokenValidator.php(66): League\OAuth2\Server\Exception\OAuthServerException::accessDenied('Access token ha...')
#1 /data/www/public_html/xxxx/vendor/league/oauth2-server/src/ResourceServer.php(82): League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator->validateAuthorization(Object(Zend\Diactoros\ServerRequest))
......

当我在line 66 检查文件vendor\league\oauth2-server\src\AuthorizationValidators\BearerTokenValidator.php 时。似乎我的访问令牌已被吊销,但在我的数据库revoked 列中仍然是假的,而且这个访问令牌是全新的,我是在几分钟前创建的。

if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
    throw OAuthServerException::accessDenied('Access token has been revoked');
}

有什么想法吗?

【问题讨论】:

    标签: mongodb laravel-5 laravel-passport


    【解决方案1】:

    在深入研究vendor\league\oauth2-server\src\AuthorizationValidators\BearerTokenValidator.php 几个小时后,我找到了解决方案。

    问题出在文件vendor\laravel\passport\src\TokenRepository.php 函数public function find($id) 第27 行

    public function find($id)
    {
        return Token::find($id);
    }
    

    因为我使用 Mobodb 而这个函数是使用 use Jenssegers\Mongodb\Eloquent\Model 并且它搜索 _id 而不是 id。所以,只需要像这样进行更改查找功能。

    public function find($id)
    {
        return Token::where('id', $id)->first();
    }
    

    虽然不建议修改vendor文件,但在这种情况下我必须这样做,希望在下一个版本中,Laravel护照的作者会发布一个护照支持mongoDB。

    希望这对某人有所帮助。

    【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2019-05-21
    • 2017-01-06
    • 2019-02-28
    • 2019-06-14
    • 2017-04-24
    • 2013-06-28
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多