Laravel 5.4 mongodb 护照访问令牌未经身份验证
Laravel 5.4 mongodb passport access token Unauthenticated
我已经使用 mongodb (Jenssegers Mongodb) and I use passport in Laravel 5.4 follow by this document.
构建了一个示例 Laravel 项目
一切正常,直到我将访问令牌交给 Postman 进行测试,现在看看我的 api.php
路由
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
在 postman 中,我设置了两个 headers,即 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');
}
有什么想法吗?
经过几个小时的深入研究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.
希望这对某人有所帮助。
我已经使用 mongodb (Jenssegers Mongodb) and I use passport in Laravel 5.4 follow by this document.
构建了一个示例 Laravel 项目一切正常,直到我将访问令牌交给 Postman 进行测试,现在看看我的 api.php
路由
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
在 postman 中,我设置了两个 headers,即 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');
}
有什么想法吗?
经过几个小时的深入研究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.
希望这对某人有所帮助。