Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
Rest AngularJS$http POST withCredentials失败,请求正文中有数据_Rest_Session Cookies_Cors_Preflight_Angularjs Http - Fatal编程技术网

Rest AngularJS$http POST withCredentials失败,请求正文中有数据

Rest AngularJS$http POST withCredentials失败,请求正文中有数据,rest,session-cookies,cors,preflight,angularjs-http,Rest,Session Cookies,Cors,Preflight,Angularjs Http,AngularJS使用REST对服务器端进行身份验证,并获取JSESSIONID cookie。 在下一步中,我将尝试使用REST和上一步获得的会话cookie从服务器端获取一些JSON数据。以下是客户端代码: getSomeJSONDataFromServer:function() { var deferred = $q.defer(); $http({ method: 'POST', withCredentials: true, url: "http://domain

AngularJS使用REST对服务器端进行身份验证,并获取JSESSIONID cookie。 在下一步中,我将尝试使用REST和上一步获得的会话cookie从服务器端获取一些JSON数据。以下是客户端代码:

getSomeJSONDataFromServer:function() {
var deferred = $q.defer();
$http({
    method: 'POST',
    withCredentials: true,
    url: "http://domain.name/app/someURL",
    headers:{
        'Accept':'application/json',
        'Content-Type':'application/json; charset=utf-8',
        'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
    }
})
.success(function(data, status, headers, config) {
    // handle data
})
.error(function(data, status, headers, config) {
    // handle error
});
return deferred.promise;
}
以上代码正常工作:

当我在上面的POST请求正文中发送一些数据时,问题就开始了

...
$http({
    method: 'POST',
    withCredentials: true,
    url: "http://domain.name/app/someURL",
    headers:{
        'Accept':'application/json',
        'Content-Type':'application/json; charset=utf-8',
        'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
    },
    data: '{}'
})
.success(...
上述代码在预照明请求中失败:

服务器似乎启动了一个新会话,因为会话cookie由于某种原因未发送。无论如何,我觉得我错过了一些非常简单的东西,一些标题或类似的东西。。。 欢迎提出任何意见。提前感谢。

根据凭据,即会话cookie不会在飞行前请求中发送,因此解决方案是在REST服务器端禁用选项请求的安全性,只允许不带会话cookie的请求用于选项请求

当然,要注意不要禁用POST和GET请求的安全性。

根据

为了在POST中允许cookie,需要使用访问控制允许凭据配置远程:true

如果您使用的是jetty,则需要配置

    <init-param>
        <param-name>allowCredentials</param-name>
        <param-value>true</param-value>
    </init-param>

allowCredentials
真的
引用MDN的文章

重要注意事项:响应认证请求时,服务器必须指定域,并且不能使用通配符。如果标题通配符为:Access Control Allow Origin:*,则上述示例将失败。由于Access Control Allow Origin明确提到,凭证识别内容将返回给调用的web内容。

Amir

请允许我帮助您和其他人清楚了解与CORS打交道时的期望,例如:

索引:
  • 简单CORS版本
  • 预飞CORS版本
  • 具有凭据的请求
简单CORS版本
  • 您可以使用GET、HEAD或POST:
    • 在任何方法情况下,都不能设置自定义标题
    • 您可以始终让服务器端通过access Control Allow Origin标头限制域访问
  • 如果POST有请求数据:
    • 内容类型必须是以下任一项:
      • 应用程序/x-www-form-urlencoded
      • 多部分/表单数据
      • 文本/html
  • 这个简单的版本显然不是您的情况…

    预飞CORS版本
  • 您可以使用GET、HEAD或POST:
    • 在任何方法情况下,都可以设置自定义标题
    • 您可以始终让服务器端通过access Control Allow Origin标头限制域访问
  • 如果POST有请求数据:
    • 内容类型可以是以下内容之一:
      • 应用程序/x-www-form-urlencoded
      • 多部分/表单数据
      • 文本/html
    • 内容类型也可以是其他类型的
      • 应用程序/json
      • 应用程序/xml
      • 文本/xml
      • 等等
  • 这个预选版本显然是您的案例…

    具有凭据的请求
  • 如果您使用cookies调用调用,则需要在请求中设置以下参数部分:
    • withCredentials=true
  • 带有凭据的请求显然也是您的情况…


    这似乎是一个跨域调用的问题。如果您正在调用的url与您的调用脚本位于同一个域中,AJAX调用只会发送Cookies。是的,肯定是CORS调用。直到你把身体添加到请求中。。。