php 总是得到“消息”:“未经认证”--拉腊维尔护照

k3fezbri  于 2023-03-11  发布在  PHP
关注(0)|答案(8)|浏览(119)

我已经找到了许多教程这一整天。我的设置是完全相同的所有基本教程在那里。
目前,我能够访问http://localhost/oauth/token,并成功将令牌返回给我。
之后,我使用ARC(高级Rest客户端)来做调用我自己的API的测试。
我传递了标题,例如

Authorization: Bearer the_token_here
accept: application/json

从这个头文件中,我只想访问laravel /user提供的默认API。
但是,我总是得到{ "message": "Unauthenticated." }的响应
请参阅本教程https://itsolutionstuff.com/post/laravel-5-how-to-create-api-authentication-using-passport-example.html
我可以按照教程进行登录,但无法通过端点details获取数据。它返回{ "message": "Unauthenticated." }响应
我的路线api.php

Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function(){
    Route::get('/user', function( Request $request ){
        return $request->user();
    });
});

顺便说一句,在laravel.log中没有错误消息,我已经设置为调试模式

更新感谢Mayank提出的意见

League\\OAuth2\\Server\\Exception\\OAuthServerException: The resource owner or authorization server denied the request. in /.../vendor/league/oauth2-server/src/Exception/OAuthServerException.php:173
Stack trace:
#0 /.../vendor/league/oauth2-server/src/AuthorizationValidators/BearerTokenValidator.php(59): League\\OAuth2\\Server\\Exception\\OAuthServerException::accessDenied('Missing "Author...')
#1 /.../vendor/league/oauth2-server/src/ResourceServer.php(82): League\\OAuth2\\Server\\AuthorizationValidators\\BearerTokenValidator->validateAuthorization(Object(Zend\\Diactoros\\ServerRequest))
#2 /.../vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php(46): League\\OAuth2\\Server\\ResourceServer->validateAuthenticatedRequest(Object(Zend\\Diactoros\\ServerRequest))
11dmarpk

11dmarpk1#

为了获得原因的详细错误消息,您需要转到CheckClientCredentials类详细信息,如下所示

public function handle($request, Closure $next, ...$scopes)
{
    $psr = (new DiactorosFactory)->createRequest($request);

    try {
        $psr = $this->server->validateAuthenticatedRequest($psr);
    } catch (OAuthServerException $e) {
        error_log($e->getHint()); // add this line to know the actual error
        throw new AuthenticationException;
    }

    $this->validateScopes($psr, $scopes);

    return $next($request);
}

根据错误信息。在我的问题。
解决方案是将其添加到根文件夹的.htaccess(不仅在公共文件夹内)

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

这里提到的官方文件中也有一条注解
如果没有上述配置,从任意位置调用应用程序时将忽略Authorization标头。一旦忽略,类内部将无法检索此标头数据

yhuiod9q

yhuiod9q2#

如果你已经尝试了所有的方法,但似乎都不起作用,那么试着清除你的配置缓存。我花了两天时间重新安装passport,学习了数十亿个教程,创建了测试项目等等,最终意识到我需要清除缓存
php artisan config:cache

3htmauhk

3htmauhk3#

在Ubuntu中,执行以下操作。
启用重写模式。
sudo a2enmod rewrite
转到cd /etc/apache2
然后打开apache2.confnano apache2.conf,找到下面的行,将AllowOverride None更改为AllowOverride All,如下所示。

# /etc/apache2/apache2.conf
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

最后,重新启动apache2服务器
sudo service apache2 restart

vfhzx4xs

vfhzx4xs4#

将以下代码粘贴到项目根文件夹的.htaccess文件中。

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1
gopyfrb3

gopyfrb35#

我有同样的问题与Laravel 8 + Postman ,我是加密密码在数据库中的Bcypt(默认laravel加密密码的用户模型)。我是解决这个问题。

u3r8eeie

u3r8eeie6#

对于那些得到错误消息Unauthenticated即使令牌是正确的,只需替换laravel 8预建路由API:

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

变成

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

nhaq1z217#

如果任何人有相同的问题,并且所选的解决方案确实解决了它,请检查以下内容:**如果你去总是响应{“消息”:“Unauthenticated.”}**解决方案正在将此添加到根文件夹的.htaccess中(不仅在公用文件夹中)
选项-多视图-索引

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
k2arahey

k2arahey8#

如果任何人遇到相同的问题,而所选的解决方案无法解决它,请检查以下内容:
1)检查您是否在请求的头中发送了X-CSRF-TOKEN。在我的例子中,我使用了带有axios的vue:

let token = window.$('meta[name="csrf-token"]').attr('content');
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token;

如果您正在发送,请尝试更改 vendor/laravel/passport/src/Passport.php 第125行中的值(可能会更改)
正确错误

public static $unserializesCookies = false;

此问题可能与https://github.com/laravel/passport/issues/452中的问题类似
关于序列化的解释在期刊中

更新日期:2020年2月1日

正如 Zac Grierson 所评论的,供应商文件不应修改,因为它们将在以下内容中更改
composer 更新

  • micksp* 找到了更好的解决方案:“添加受保护的静态$serialize = false;添加到您的应用程序/Http/Middleware/EncryptCookies. php。然后删除您的浏览器Cookie。

相关问题