Java 未在后续请求上传递Cookie

Java 未在后续请求上传递Cookie,java,javascript,ajax,rest,spring-mvc,Java,Javascript,Ajax,Rest,Spring Mvc,在尝试使用AJAX向SpringMVC控制器发出后续请求时,我在传递cookie时遇到问题 例如,我有一个/login端点,我通过POST方法向其传递一些JSON,它设置cookie。我在firebug中看到了cookie: 正如你所看到的,它就在那里。我正在创建这样一个cookie: NewCookie cookie = new NewCookie(new Cookie(SECURITY_TICKET, encodedTicket, configKey, null), null, (int)

在尝试使用AJAX向SpringMVC控制器发出后续请求时,我在传递cookie时遇到问题

例如,我有一个
/login
端点,我通过POST方法向其传递一些JSON,它设置cookie。我在firebug中看到了cookie:

正如你所看到的,它就在那里。我正在创建这样一个cookie:

NewCookie cookie = new NewCookie(new Cookie(SECURITY_TICKET, encodedTicket, configKey, null), null, (int) TimeUnit.MINUTES.toSeconds(expireTime), expireTimeDate, false, false)
ResponseEntity entity = new ResponseEntity<>(this.entity, this.httpHeaders, this.status)
$(function () {
    $.ajax({
        url: 'http://localhost:8080/dev-citigroup-citi/login',
        type: 'POST',
        dataType: 'json',
        data: '{ "username": "client1", "password": "*******", "platform": "iOS", "UUID": "321321321", "application": "CitiReach", "applicationVersion": "1.0" }',
        success: function (data, status, xhr) {
            $.ajax({
                url: 'http://localhost:8080/dev-citigroup-citi/search/eventattendee?q=*:*&start=0&rows=1&wt=json&indent=true',
                type: 'GET',
                xhrFields: { withCredentials:true },
                success: function (data, status, xhr) {
                    console.log(data);
                },
                error: function (jqXHR) {
                    console.log(jqXHR);
                }
            });
        },
        error: function (jqXHR) {
            console.log(jqXHR);
        }
    });
});
并将其设置为HTTP标头:

httpHeaders.add(HttpHeaders.SET_COOKIE, cookie.toString());
然后将这些标题添加到ResponseEntity,如下所示:

NewCookie cookie = new NewCookie(new Cookie(SECURITY_TICKET, encodedTicket, configKey, null), null, (int) TimeUnit.MINUTES.toSeconds(expireTime), expireTimeDate, false, false)
ResponseEntity entity = new ResponseEntity<>(this.entity, this.httpHeaders, this.status)
$(function () {
    $.ajax({
        url: 'http://localhost:8080/dev-citigroup-citi/login',
        type: 'POST',
        dataType: 'json',
        data: '{ "username": "client1", "password": "*******", "platform": "iOS", "UUID": "321321321", "application": "CitiReach", "applicationVersion": "1.0" }',
        success: function (data, status, xhr) {
            $.ajax({
                url: 'http://localhost:8080/dev-citigroup-citi/search/eventattendee?q=*:*&start=0&rows=1&wt=json&indent=true',
                type: 'GET',
                xhrFields: { withCredentials:true },
                success: function (data, status, xhr) {
                    console.log(data);
                },
                error: function (jqXHR) {
                    console.log(jqXHR);
                }
            });
        },
        error: function (jqXHR) {
            console.log(jqXHR);
        }
    });
});
正如我提到的,
/login
正常,但另一个调用失败。我正在添加
xhrFields:{withCredentials:true}
,它应该包括
/search
请求中的cookie,但由于某些原因它不是

我还正确设置了CORS:

Access-Control-Allow-Origin: http://localhost:63342
Access-Control-Allow-Credentials: true
http://localhost:63342
是请求的来源,它在CORS标头中配置


知道这里可能有什么问题吗?

我找到了解决方案,因此,我没有在方法级别使用
xhrFields:{withCredentials:true}
,而是将其放在全局:

    $.ajaxSetup({
        xhrFields: {
            withCredentials: true
        }
    });
cookie现在在后续请求中传递。不知道为什么它在方法级别不起作用,可能是jquery中的一个bug

我使用的jQuery版本:2.1.3

也许这会对将来的人有所帮助