带有axios的X-XSRF-TOKEN标头

时间:2018-10-17 21:37:38

标签: axios csrf x-xsrf-token

如果我设置了X-XSRF-TOKEN Cookie服务器端,是否需要设置任何内容来发送XSRF-TOKEN头?

https://github.com/axios/axios/blob/master/lib/defaults.js#L74 https://github.com/axios/axios/blob/master/dist/axios.js#L1072

读取的内容好像不是,但我看不到有人出去。

我要补充一点,是我已将withCredentials设置为true,所以我的确符合OR中的第一个要求:

var xsrfValue = (config.withCredentials || isURLSameOrigin(config.url)) && config.xsrfCookieName ?
            cookies.read(config.xsrfCookieName) :
            undefined;

          if (xsrfValue) {
            requestHeaders[config.xsrfHeaderName] = xsrfValue;
}

因此,如果config.xsrfCookieName是默认值.....

更新:

因此,我的OPTIONS预检CORS正在工作,就像现在的POST一样,但是没有X-XSRF-TOKEN被发送。

  methods: {
    onSubmit(e) {
      this.axios
        .post(
          e.target.action,
          { data: this.form },
          {
            withCredentials: true,
            xsrfCookieName: "XSRF-TOKEN",
            xsrfHeaderName: "X-XSRF-TOKEN"
          }
        )
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          this.errors.push(err)
        })
    }
  }

谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并且是关于Cookie上的“安全”标志的,就像在请求的“ cookies”标签上可以看到的那样,但是没有显示在“ application”标签下的cookie上:

XSRF-TOKEN secure

就我而言,我必须要求后端将其设置下来。 发生这种情况是因为,为了安全起见,您无法通过javascript访问它。

document.cookie // is empty
相关问题