What is the right way to use angular2 http requests with Django CSRF protection?

后端 未结 7 736
独厮守ぢ
独厮守ぢ 2020-11-28 13:23

In Angular1 the problem can be solved by configuring $http-provider. Like:

app.config(function($httpProvider) {
  $httpProvider.defaults.xsrfCookieName = \'c         


        
相关标签:
7条回答
  • 2020-11-28 14:17

    Now that Angular 2 is released the following seems to be the correct way of doing this, by using CookieXSRFStrategy.

    I've configured my application to have a core module but you can do the same in your main application module instead:

    import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
    import { CommonModule }   from '@angular/common';
    import { HttpModule, XSRFStrategy, CookieXSRFStrategy } from '@angular/http';
    
    @NgModule({
        imports: [
            CommonModule,
            HttpModule
         ],
        declarations: [ ],
        exports: [ ],
        providers: [
            {
                provide: XSRFStrategy,
                useValue: new CookieXSRFStrategy('csrftoken', 'X-CSRFToken')
            }
        ]
    })
    
    
    export class CoreModule {
    }, 
    
    0 讨论(0)
提交回复
热议问题