我正在为Nuxt.js使用Axios模块。
我确实对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));
复制步骤-根据要求:
我在$axios.onRequest
中打印了请求配置之后,正如我们从上面看到的,所有设置都已设置,但是在联网中根本没有
再往后的后端服务器未收到任何标头:
带有控制台日志:
`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.`
有什么想法吗?