【发布时间】: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/json 和Authorization: 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