CSRF - Axios удаляет заголовки - PullRequest
0 голосов
/ 06 июля 2019

Я использую модуль Axios для Nuxt.js.

У меня проблема с токеном CSRF.Всякий раз, когда я вхожу в приложение, я не могу отправить любой запрос POST.Ответ всегда 403.

Конфигурация Axios:

Внутри nuxt.config.js

axios: {
    debug: true,
    credentials: true,
    baseURL: `${process.env.API_PROTOCOL}://${process.env.API_HOST}${process.env.API_PORT ? `:${process.env.API_PORT}` : ''}${process.env.API_PREFIX}`,
},

Внутри плагина / перехватчика 'axios.js'

const EXPIRED_TOKEN_MESSAGE = 'Expired JWT Token';

export default function ({
    $axios, redirect, store,
}) {
    $axios.setHeader('Content-Type', 'application/json');
    $axios.setHeader('Accept', 'application/json');


    $axios.onRequest((config) => {
        const configLocal = config;

        if (config.method === 'post') {
            configLocal.headers['X-Requested-With'] = 'XMLHttpRequest';
            configLocal.headers['x-csrf-token'] = store.state.authentication.csrf;
        }
    });

    $axios.onError((error) => {
        const { response: { data: { message } } } = error;
        // const msg = status === 500 ? 'Internal Server Error' : message;
        if (message === EXPIRED_TOKEN_MESSAGE && store.state.authentication.csrf) {
            store.dispatch('authentication/logout');
            return redirect('/');
        }

        if (process.client) {
            // store.dispatch('alerts/addAlert', { type: 'error', message: msg });
        }

        return Promise.reject(error.response);
    });
}

Запрос вызова:

this.app.$axios.$post('resources/add', data).then(() => {
        }).catch(e => console.log(e));

Этапы воспроизведения - подкапот запроса:

enter image description here

После того, как я напечатал конфигурацию запроса внутри $axios.onRequest, как мы видим сверху, все установлено, но в сети не ввсе

enter image description here

В дальнейшем внутренний сервер не получает заголовки:

enter image description here

С консольным журналом:

`2019-07-04 18:45:18.380 DEBUG 712 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : No HttpSession currently exists
2019-07-04 18:45:18.380 DEBUG 712 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : No SecurityContext was available from the HttpSession: null. A new one will be created.`

Есть идеи что не так?

...