Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取并插入Django在Angular中设置的CSRF cookie值_Django_Angular_Post_Django Rest Framework_Csrf - Fatal编程技术网

获取并插入Django在Angular中设置的CSRF cookie值

获取并插入Django在Angular中设置的CSRF cookie值,django,angular,post,django-rest-framework,csrf,Django,Angular,Post,Django Rest Framework,Csrf,我在Django和Angular之间的CSRF集成方面遇到了一些问题。我早些时候发表了一篇更广泛的文章。问题是登录用户不能从前端发出post请求,但他们可以通过表单从Django的可浏览API发出完全相同的请求 我在Django服务的索引模板上设置了{%csrf_token%},并使用Angular的Build it XSRFS策略将攻击头设置为http请求,但我认为问题在于,我没有捕捉到Django生成的csrf令牌提供的值,也不知道如何实现收集数据并将其附加到我的http请求cookie。以

我在Django和Angular之间的CSRF集成方面遇到了一些问题。我早些时候发表了一篇更广泛的文章。问题是登录用户不能从前端发出post请求,但他们可以通过表单从Django的可浏览API发出完全相同的请求

我在Django服务的索引模板上设置了{%csrf_token%},并使用Angular的Build it XSRFS策略将攻击头设置为http请求,但我认为问题在于,我没有捕捉到Django生成的csrf令牌提供的值,也不知道如何实现收集数据并将其附加到我的http请求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>
确保csrfmidlewaretoken:{{csrf_token}}与表单一起提交


确保{%csrf_token%}位于标记内。如果您使用ajax发布数据,请将csrfmidlewaretoken:{{csrf_token}}添加到您提交的表单数据。

就是这样,因为Django只处理index.html,所以我无法将{%csrf_token%}附加到主页之外的任何表单。但是,由于每次加载新模板时该值都会更改,因此是否可以?我将尝试将其附加到我的表单数据中。结果表明,该值不会更改,添加csrfmiddlewaretoken:tokenvalue没有任何帮助,尽管它也没有造成任何伤害,但没有发生额外的错误。不过,您确实回答了我关于如何执行该操作的问题,谢谢!