微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Angular的cookie中处理csrf令牌

如何解决如何在Angular的cookie中处理csrf令牌

TS

constructor(
private global: GlobalService
) { }

  login() {
const data = {
'username': 'admin','password': 'admin@123'
};
    this.global.postData('users/',data).pipe(take(1)).subscribe((x: any) => {
              console.log(x);
            });
    }

global.service.ts

 postData(url: string,data: any,params?: any) {
    return this.httpClient.post(url,data,Object.assign(this.getHeaders(),params || {})).pipe(take(1));
  }

  putData(url: string,params?: any) {
    return this.httpClient.put(url,params || {})).pipe(take(1));
  }
private getHeaders() {
    // tslint:disable-next-line:max-line-length

    // const credentials = this.creds.credentials;
    const headers = {
      'Content-Type': 'application/json','Access-Control-Allow-Origin': '*'
    };

    const _creds = sessionStorage.getItem(environment.appKey + '-credentials') ||
      localStorage.getItem(environment.appKey + '-credentials');
    const credentials = _creds ? JSON.parse(_creds) : null;

    if (credentials && credentials.token) {
      headers['Authorization'] = 'Bearer ' + credentials.token;
    }

    return {
      headers: new HttpHeaders(headers)
    };
  }

enter image description here

在这里要做的是获取csrf令牌。 但缺少csrf令牌。我还从HttpClientXsrfModule添加appModule

当单击登录功能时,它也应该显示cookie中的csrftoken。

有效载荷

{
 csrfmiddletoken:
 username: admin
 password: admin123
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。