这个RequestVerificationToken 是预防CSRF攻击的一个手段,abp默认是开启的。开启后,前端请求(非GET)的header必须带有这个token,请求才能到达对应方法里。
1、前端添加header参数(推荐)
ABP默认会返回一个Cookie XSRF-TOKEN
,在前端提交的header里附带RequestVerificationToken即可。
如果使用的前端框架为vue-vben-admin,可在utils/http/axios/Axios.ts中的supportFormData方法中设置
supportFormData(config: AxiosRequestConfig) { const headers = config.headers || this.options.headers; const contentType = headers?.['Content-Type'] || headers?.['content-type']; //AntiForgery设置 解决"RequestVerificationToken" is not present报错 config.xsrfCookieName = 'XSRF-TOKEN'; config.xsrfHeaderName = 'RequestVerificationToken'; //end if ( contentType !== ContentTypeEnum.FORM_URLENCODED || !Reflect.has(config, 'data') || config.method?.toUpperCase() === RequestEnum.GET ) { return config; } return { ...config, data: qs.stringify(config.data, { arrayFormat: 'brackets' }), }; }
2、ABP后台关闭AntiForgery设置
如想关闭此配置,只需要在host工程下面的modules中添加如下方法,并在ConfigureServices中引用即可。
public void ConfigureAntiForgery() { Configure<AbpAntiForgeryOptions>(options => { options.AutoValidateIgnoredHttpMethods.Add("POST"); options.AutoValidateIgnoredHttpMethods.Add("DELETE"); }); }