如何在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 (将#修改为@)