@@ -149,7 +149,10 @@ function $HttpProvider() {
149
149
} ,
150
150
post : { 'Content-Type' : 'application/json;charset=utf-8' } ,
151
151
put : { 'Content-Type' : 'application/json;charset=utf-8' }
152
- }
152
+ } ,
153
+
154
+ xsrfCookieName : 'XSRF-TOKEN' ,
155
+ xsrfHeaderName : 'X-XSRF-TOKEN'
153
156
} ;
154
157
155
158
var providerResponseInterceptors = this . responseInterceptors = [ ] ;
@@ -383,9 +386,10 @@ function $HttpProvider() {
383
386
* {@link http://en.wikipedia.org/wiki/Cross-site_request_forgery XSRF} is a technique by which
384
387
* an unauthorized site can gain your user's private data. Angular provides following mechanism
385
388
* to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie
386
- * called `XSRF-TOKEN` and sets it as the HTTP header `X-XSRF-TOKEN`. Since only JavaScript that
387
- * runs on your domain could read the cookie, your server can be assured that the XHR came from
388
- * JavaScript running on your domain. The header will not be set for cross-domain requests.
389
+ * (by default, `XSRF-TOKEN`) and sets it as an HTTP header (`X-XSRF-TOKEN`). Since only
390
+ * JavaScript that runs on your domain could read the cookie, your server can be assured that
391
+ * the XHR came from JavaScript running on your domain. The header will not be set for
392
+ * cross-domain requests.
389
393
*
390
394
* To take advantage of this, your server needs to set a token in a JavaScript readable session
391
395
* cookie called `XSRF-TOKEN` on first HTTP GET request. On subsequent non-GET requests the
@@ -395,6 +399,9 @@ function $HttpProvider() {
395
399
* up its own tokens). We recommend that the token is a digest of your site's authentication
396
400
* cookie with {@link http://en.wikipedia.org/wiki/Rainbow_table salt for added security}.
397
401
*
402
+ * The name of the headers can be specified using the xsrfHeaderName and xsrfCookieName
403
+ * properties of either $httpProvider.defaults, or the per-request config object.
404
+ *
398
405
*
399
406
* @param {object } config Object describing the request to be made and how it should be
400
407
* processed. The object has following properties:
@@ -405,6 +412,8 @@ function $HttpProvider() {
405
412
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
406
413
* - **data** – `{string|Object}` – Data to be sent as the request message data.
407
414
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
415
+ * - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
416
+ * - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
408
417
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
409
418
* transform function or an array of such functions. The transform function takes the http
410
419
* request body and headers and returns its transformed (typically serialized) version.
@@ -513,12 +522,17 @@ function $HttpProvider() {
513
522
function $http ( config ) {
514
523
config . method = uppercase ( config . method ) ;
515
524
525
+ var xsrfHeader = { } ,
526
+ xsrfCookieName = config . xsrfCookieName || defaults . xsrfCookieName ,
527
+ xsrfHeaderName = config . xsrfHeaderName || defaults . xsrfHeaderName ,
528
+ xsrfToken = isSameDomain ( config . url , $browser . url ( ) ) ?
529
+ $browser . cookies ( ) [ xsrfCookieName ] : undefined ;
530
+ xsrfHeader [ xsrfHeaderName ] = xsrfToken ;
531
+
516
532
var reqTransformFn = config . transformRequest || defaults . transformRequest ,
517
533
respTransformFn = config . transformResponse || defaults . transformResponse ,
518
534
defHeaders = defaults . headers ,
519
- xsrfToken = isSameDomain ( config . url , $browser . url ( ) ) ?
520
- $browser . cookies ( ) [ 'XSRF-TOKEN' ] : undefined ,
521
- reqHeaders = extend ( { 'X-XSRF-TOKEN' : xsrfToken } ,
535
+ reqHeaders = extend ( xsrfHeader ,
522
536
defHeaders . common , defHeaders [ lowercase ( config . method ) ] , config . headers ) ,
523
537
reqData = transformData ( config . data , headersGetter ( reqHeaders ) , reqTransformFn ) ,
524
538
promise ;
0 commit comments