作曲家.json:
"require": {
"ext-json": "*",
"doctrine/dbal": "^2.12.1",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"laravel/framework": "^8",
"laravel/sanctum": "^2.9",
"laravel/tinker": "^2.5",
"spatie/laravel-permission": "^3.18"
},
内核.php:
protected $middlewareGroups = [
'web' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
// ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
],
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:api',
SubstituteBindings::class,
],
];
Cors.php:
return [
'paths' => ['api/*, /login'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
];
圣所.php:
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', implode(',', [
'localhost',
'localhost:8000',
'localhost:8080',
'localhost:8002',
'127.0.0.1:8002',
'127.0.0.1:8080',
]))),
auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
RouteServiceProvider.php:
Route::prefix('api/v1')
->middleware('auth:sanctum')
->group(function() {
require_once base_path('routes/Api/V1/Auth/routes.php');
require_once base_path('routes/Api/V1/Users/routes.php');
});
爱讯:
import Vue, { PluginObject } from 'vue';
import axios from 'axios';
const config = {};
const _axios = axios.create(config);
_axios.defaults.withCredentials = true;
_axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
_axios.interceptors.request.use(
cfg => {
return cfg;
},
err => {
return Promise.reject(err);
},
);
/**
* Add a response interceptor
*/
_axios.interceptors.response.use(
res => {
return res;
},
err => {
return Promise.reject(err);
},
);
const Plugin: PluginObject<any> = {
install: Vue => {
Vue.$axios = _axios;
},
};
Plugin.install = Vue => {
Vue.$axios = _axios;
window.axios = _axios;
Object.defineProperties(Vue.prototype, {
$axios: {
get() {
return _axios;
},
},
});
};
Vue.use(Plugin);
export default Plugin;
我正在登录。但任何请求通过auth:sanctum
:
{message: "Unauthenticated."}
可能是什么问题呢?
错误
有两个错误:
middleware('api')
;middleware
。解决方案
我的路线是分开的
app/Providers/RouteServiceProvider.php
,对于每条路线,我middleware
都这样设置:事实证明,这在laravel 8中不起作用。
当我通过数组时,它对我来说工作正常
middleware
。此外,middleware
这个数组中的顺序很重要。