【问题标题】:Getting and inserting the value of a CSRF cookie set by Django in Angular获取和插入 Django 在 Angular 中设置的 CSRF cookie 的值
【发布时间】:2018-11-05 00:34:57
【问题描述】:

我在让 Django 和 Angular 之间的 CSRF 集成工作时遇到了一些麻烦。我之前发表了一篇更广泛的帖子about that here.。问题是登录用户无法从前端发出 post-requests,但他们可以通过表单从 Django 的可浏览 API 发出完全相同的请求。

我在 Django 服务的索引模板上设置了一个 {% csrf_token %} 并且我正在使用 Angular 构建的 XSRFS 策略来设置对 http 请求的攻击标头,但我认为问题在于我没有捕捉到由 Django 生成的 csrf 令牌提供的值,并且无法弄清楚如何实现收集该数据并将其附加到我的 Angular http-request cookie。这是提供策略的模块的代码:

import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { HttpModule, XSRFStrategy, CookieXSRFStrategy } from '@angular/http'

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { RegisterComponent } from './register/register.component';
import { LoginComponent } from './login/login.component';
import { AlertComponent } from './_directives/alert.component';
import { ProfileComponent } from './profile/profile.component';
import { AuthGuardService } from './_guards/auth-guard.service';
import { AlertService } from './_services/alert.service';
import { AuthService } from './_services/auth.service';
import { UserService } from './_services/User.service';

@NgModule({
  declarations: [
    AppComponent,
    RegisterComponent,
    LoginComponent,
    AlertComponent,
    ProfileComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    HttpClientModule,
    HttpModule
  ],
  providers: [
    {
      provide: XSRFStrategy,
      useValue: new CookieXSRFStrategy('csrftoken', 'X-CSRFToken')
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是来自浏览器的输出,带有我认为需要以某种方式附加的 csrf 值:

<input type='hidden' name='csrfmiddlewaretoken' value='9FKUKSXHbJfLClZniDXIHrMu2AEUtPQr4mGDfNSOZURHYVyKGBYvREIuucN0UsEM' />
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularWebapp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
  <app-root></app-root>

<script type="text/javascript" src="/static/runtime.js"></script><script type="text/javascript" src="/static/polyfills.js"></script><script type="text/javascript" src="/static/styles.js"></script><script type="text/javascript" src="/static/vendor.js"></script><script type="text/javascript" src="/static/main.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</body>
</html>

【问题讨论】:

    标签: django angular post django-rest-framework csrf


    【解决方案1】:

    确保"csrfmidlewaretoken": {{csrf_token}} 与表单一起提交。

    确保{% csrf_token %}&lt;form&gt;&lt;/form&gt; 标签内。如果您使用 ajax 发布数据,请将 "csrfmidlewaretoken": {{csrf_token}} 添加到您提交的表单数据中。

    【讨论】:

    • 就是这样,因为 Django 只处理 index.html 我不能将 {% csrf_token %} 附加到主页之外的任何表单。但是,由于每次加载新模板时值都会更改,所以可能没问题?我将尝试将其附加到我的表单数据中。
    • 原来值没有变化,加上 "csrfmiddlewaretoken":tokenvalue 也没有用,虽然也没有什么坏处,也没有出现额外的错误。
    • 你确实回答了我关于如何做到这一点的问题,谢谢!
    猜你喜欢
    • 2017-04-15
    • 2014-05-09
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2017-05-05
    • 2020-12-05
    • 2015-05-22
    相关资源
    最近更新 更多