Axios + Ionic React + Sails:_csrf令牌403禁止使用

问题描述 投票:1回答:1

我正在使用Sails.js开发API,并通过Ionic-React开发用户应用。在页面加载时,我发出axios请求以获取_csrf令牌。当我从登录表单向帆提交数据时,我总是收到403禁止响应。我在航行中禁用了csrf(config / security.js),然后可以检索响应。我正在标题中发送令牌。

我也试图获取会话cookie,但它不起作用,我认为这可能是服务器拒绝请求的原因。

Ionic App:

componentDidMount(this: this) {
axios.get('http://localhost:1337/api/v1/security/grant-csrf-token')
  .then(response => {
    const _csrf = response.data._csrf
    this.setState({
      form: {
        ...this.state.form,
        _csrf: _csrf,
      }})
  });
}

提交时:

const { emailAddress, password, _csrf } = this.state.form;
const config= {
  data: {
    "emailAddress": emailAddress,
    "password": password,
  },
  headers: {
    "x-csrf-token": _csrf
  },
  withCredentials: true,
  jar:cookieJar,
};

axios.post('http://localhost:1337/api/v1/users/authenticate', null, config)
.then(res => {
  console.log(res);
})
.catch(err => {
  console.log(err);
})};

关于Chrome DevTools网络响应:

enter image description here

在邮递员上,相同的请求有效,并且我收到200的用户数据,并且该请求确实包含sails.sid cookie。

我不想禁用csrf保护,这不是解决方案。我丢失的是sails.sid cookie吗?

ionic-framework cookies axios sails.js csrf
1个回答
0
投票

我正在使用此,

axios({
                method: 'post',
                crossdomain: true,
                url: apiFormUrl,
                data: formData,
                headers: {
                    "Content-Type": "multipart/form-data",
                    "Authorization": access_token
                }
            })
                .then

并且有效

© www.soinside.com 2019 - 2024. All rights reserved.