umi-request icon indicating copy to clipboard operation
umi-request copied to clipboard

umi-request不能自动将cookie中的 CSRF 令牌自动添加的 Headers 中

Open vini123 opened this issue 4 years ago • 4 comments

umi-request不能自动将 cookie中的 CSRF 令牌自动添加的 Headers 中。

将 cookies 中的 CSRF 令牌 XSRF-TOKEN 自动添加到 Request 的 Headers 中很有必要的。

Angular 和 Axios 都会自动加入。

谢谢。

vini123 avatar May 09 '20 03:05 vini123

todo

chenjsh36 avatar Jun 17 '20 07:06 chenjsh36

@chenjsh36 请问这个需求什么时候有时间搞一搞,自动加入是必要的,axios,描述中专门指出了此功能。

image

leshalv avatar Sep 22 '20 09:09 leshalv

我有点不解,CSRF的cookie名称可能时自定义的,怎么自动加到header里呢,如果手动配置一下cookie名,那和手动写到header有什么区别呢

feizhaojun avatar Mar 02 '21 09:03 feizhaojun

https://blog.csdn.net/qq_33997198/article/details/112318222 https://umijs.org/zh-CN/plugins/plugin-request#%E8%BF%90%E8%A1%8C%E6%97%B6%E9%85%8D%E7%BD%AE 在app.tsx增加这个拦截器,可以解决

const tokenIt: RequestInterceptor = (
  url: string,
  options: RequestOptionsInit
) => {
  const cookie = new Cookies();
  const opt = options;
  opt.headers['X-XSRF-TOKEN'] = cookie.get('XSRF-TOKEN');
  return {
    url,
    options: opt,
  }
}

export const request: RequestConfig = {
  // 设置请求的头信息等进行拦截
  requestInterceptors: [
    tokenIt
  ]
};

LinruoT avatar Dec 23 '21 08:12 LinruoT