@@ -149,7 +149,10 @@ function $HttpProvider() {
149149 } ,
150150 post : { 'Content-Type' : 'application/json;charset=utf-8' } ,
151151 put : { 'Content-Type' : 'application/json;charset=utf-8' }
152- }
152+ } ,
153+
154+ xsrfCookieName : 'XSRF-TOKEN' ,
155+ xsrfHeaderName : 'X-XSRF-TOKEN'
153156 } ;
154157
155158 var providerResponseInterceptors = this . responseInterceptors = [ ] ;
@@ -383,9 +386,10 @@ function $HttpProvider() {
383386 * {@link http://en.wikipedia.org/wiki/Cross-site_request_forgery XSRF} is a technique by which
384387 * an unauthorized site can gain your user's private data. Angular provides following mechanism
385388 * 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.
389393 *
390394 * To take advantage of this, your server needs to set a token in a JavaScript readable session
391395 * cookie called `XSRF-TOKEN` on first HTTP GET request. On subsequent non-GET requests the
@@ -395,6 +399,9 @@ function $HttpProvider() {
395399 * up its own tokens). We recommend that the token is a digest of your site's authentication
396400 * cookie with {@link http://en.wikipedia.org/wiki/Rainbow_table salt for added security}.
397401 *
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+ *
398405 *
399406 * @param {object } config Object describing the request to be made and how it should be
400407 * processed. The object has following properties:
@@ -405,6 +412,8 @@ function $HttpProvider() {
405412 * `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
406413 * - **data** – `{string|Object}` – Data to be sent as the request message data.
407414 * - **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.
408417 * - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
409418 * transform function or an array of such functions. The transform function takes the http
410419 * request body and headers and returns its transformed (typically serialized) version.
@@ -513,12 +522,17 @@ function $HttpProvider() {
513522 function $http ( config ) {
514523 config . method = uppercase ( config . method ) ;
515524
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+
516532 var reqTransformFn = config . transformRequest || defaults . transformRequest ,
517533 respTransformFn = config . transformResponse || defaults . transformResponse ,
518534 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 ,
522536 defHeaders . common , defHeaders [ lowercase ( config . method ) ] , config . headers ) ,
523537 reqData = transformData ( config . data , headersGetter ( reqHeaders ) , reqTransformFn ) ,
524538 promise ;
0 commit comments