Laravel 5.5 애플리케이션 (여권을 사용하지 않음)이 있고 재고 인프라를 사용하여 빌드 한 VueJS 구성 요소에서 /api
라우트에 액세스하려고합니다. 사용 vue-resource 유효한 인증 응답을받을 수 없습니다.Laravel Basic 인증 : api 401 Unauthorized
내 VueJS :
Route::middleware('auth:api')->group(function() {
Route::post('subscribe', '[email protected]');
Route::post('unsubscribe', '[email protected]');
Route::post('update-subscription', '[email protected]');
});
내가 Laravel가에서 XSRF를 처리하는 생각 :
methods: {
updateSubscription(newChannelId, oldChannelId) {
if (oldChannelId < 1 && newChannelId > 0) {
console.log('new subscription', this);
this.$http.post('/api/subscribe', {
'game_id': this.gameId,
'channel_id': newChannelId
}).then(response => {
// success
}, response => {
console.error('Failed to subscribe');
});
내 Laravel 경로 routes/api.php
의를 나는 XSRF 토큰 여기에 전달되는 볼 수 있습니다 게이트와 나는이 문제에 부딪치지 않을 것이다.
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
나는 무엇을 그리워 않은 : 나는 내 HTTP 커널의 모습 주어진 auth:api
100 %에 익숙하지 않다 생각?
의미가 있지만 ... Laravel은 API 인증 기능을 갖춘 우주선을 관리합니까? 아니면 이것을 관리하기 위해 'Passport'를 가져와야합니까? – Webnet
원하는 경우 사용자 테이블에'api_token' 필드를 추가 할 수 있습니다. 이것은 비교를 위해 일반 텍스트로 저장해야하므로 가장 안전하지는 않지만 빠른 속도로 작동합니다. – fubar
이 경우 Ajax 요청 일뿐입니다. 나는'auth : api' 토큰을 제거하고 사용자가이 요청을하기 위해서 로그인 만하면된다는 것을 알아야한다. 어쨌든 소스 코드를 볼 때 JS에 토큰을 넣을 필요는 없습니다. – Webnet