Laravel 护照 API return 未经身份验证
Laravel Passport API return Unauthenticated
我正在尝试对 laravel 应用程序中的 api 调用进行身份验证。我已经按照文档安装了 Passport,我想我没有遗漏任何东西。但是 API 调用 returns 401 未经身份验证。
Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
AuthServiceProvider.php
public function boot() {
$this->registerPolicies();
Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));
}
RouteServiceProvider
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
header
和.htaccess
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
将此代码添加到根文件夹的 .htaccess(不仅在 public 文件夹内)
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
看来您需要注册发行令牌所需的路由。简单地说,您在 boot
方法中调用 Passport::routes
方法。
public function boot() {
$this->registerPolicies();
Passport::routes();
Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));
}
如果你有任何问题,请告诉我。
我正在尝试对 laravel 应用程序中的 api 调用进行身份验证。我已经按照文档安装了 Passport,我想我没有遗漏任何东西。但是 API 调用 returns 401 未经身份验证。
Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
AuthServiceProvider.php
public function boot() {
$this->registerPolicies();
Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));
}
RouteServiceProvider
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
header
和.htaccess
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
将此代码添加到根文件夹的 .htaccess(不仅在 public 文件夹内)
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
看来您需要注册发行令牌所需的路由。简单地说,您在 boot
方法中调用 Passport::routes
方法。
public function boot() {
$this->registerPolicies();
Passport::routes();
Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));
}
如果你有任何问题,请告诉我。