Laravel 5.4 mongodb护照访问令牌未经身份验证

问题描述:

我已经使用mongodb构建了一个Laravel项目示例(Jenssegers Mongodb),并且我在Laravel 5.4中使用护照,并遵循by this documentLaravel 5.4 mongodb护照访问令牌未经身份验证

一切工作正常,直到我花了一个访问令牌邮差测试,现在看看我的api.php路线

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

在邮递员,我设置了两个头是Accept: application/jsonAuthorization: Bearer $TOKEN,我很确定我的访问令牌不是复制缺失的错误,但仍然出现错误。

{ 
    "error": "Unauthenticated." 
} 

事情我已经试过

我在模型覆盖默认idUser.php

use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasApiTokens; 

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

我还修改令牌到期时间在AuthserviceProvider.php像这样

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' 
    ]); 
} 

和其他一些方法,但仍然无法正常工作。

更新调试信息来源

当我到public/index.php添加try catch,错误apperead

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'); 
} 

任何想法?

经过几个小时深入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(); 
} 

虽然修改供应商档案不推荐,但在这种情况下,我必须这样做,希望能在接下来的版本中,Laravel护照笔者将发布一本护照的支持MongoDB的。

希望这可以帮助别人。