【发布时间】:2016-11-30 08:38:39
【问题描述】:
我正在尝试在 Wildfly 服务器上发出 REST 请求。首先,我必须登录旧应用程序并获取 cookie。该 cookie 必须在较新的应用程序中使用 这会发出 REST 请求。新应用程序是来自真实现有应用程序的一些模板。我在 REST 请求的标头中尝试了一些选项,例如设置属性 withCredentials、Access-Control-Allow-Credentials、token、crossDomain,如下面的函数 getAllEntities 所示。
经过测试的 REST 请求,它们在 Firefox 浏览器上与 RestClient 配合良好,如下所示。
我不知道该怎么做:
插入我从以前的应用程序中获得的 cookie
移除令牌(看来这是不可能的)
解决标题中写的这两个错误
这是请求在 RestClient 中的样子:
Method: GET
URL: https://xx.xx.xx.xx:8443/api/codes
Content-Type: application/json
Accept: application/json
Cookie: code_system_frontend=bvkkvbcp24igdgja4h5hht13p4; code=ae8be9267e8dfea86a8f54f6bd980733
RestClient 中的响应如下所示:
Status Code: 200 OK
Access-Control-Allow-Headers: Content-Type, Content-Disposition, Accept, responseType, X-MAC, X-TIME, Cookie
Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Type, Content-Disposition, Accept
Connection: keep-alive
Content-Type: application/json
Date: Tue, 26 Jul 2016 15:22:00 GMT
Server: WildFly/9
Transfer-Encoding: chunked
access-control-allow-credentials: true
x-powered-by: Undertow/1
和典型的 JSON 响应正文:
[
{
"code": 1,
"codename": "Demo"
},
{
"code": 2,
"codename": "Gmx"
}
//AND SO ON
]
这是 Angularjs 中的代码:
function getAllEntities(entityName, addToCache) {
config = {
headers: {
'cookie':'code_system_frontend=bvkkvbcp24igdgja4h5hht13p4;code=ae8be9267e8dfea86a8f54f6bd980733',
'Content-Type': 'application/json',
'Accept': 'application/json',
'withCredentials':'true',
//'Access-Control-Allow-Credentials': 'true',
//'X-API-TOKEN': undefined,
//'token':undefined,
//crossDomain: true
},
data: ''
};
return $http.get(endPoints.api + entityName, config)
.then(getAllEntitiesComplete)
.catch(function (message) {
exception.catcher('XHR Failed for getAllEntities for entity ' + entityName)(message);
return $q.reject(message);
logger.info('Message ' + message);
});
function getAllEntitiesComplete(data, status, headers, config) {
var entities = data.data;
if (addToCache) {
service.cachedLookups[entityName] = entities;
service[entityName + 's'] = entities;
}
return entities;
}
}
在 Firebug 中我得到:
Firebug 中的请求标头:
Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.5
Content-Type: application/json
Host: XX.XX.XX.XX:8443
Origin http://admin.dev.xxxxxxxx.xxx
Referer http://admin.dev.xxxxxxxx.xxx/xx/codes
User-Agent Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0
token fake token for anonymous user
withCredentials true
我也在 Firebug 中收到警告:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the
remote resource at https://xx.xx.xx.xx:8443/xxxxxxxx/api/code. (Reason:
CORS header 'Access-Control-Allow-Origin' does not match '*').
由于我的代码,我得到了这个错误:
Error: XHR Failed for getAllEntities for entity suborder Object { status=0, config={...}, data=null, more...}
在 Crome 我得到:
Refused to set unsafe header "cookie"
和
net::ERR_INSECURE_RESPONSE
【问题讨论】:
标签: angularjs rest cookies access-token