【问题标题】:Refused to set unsafe header "cookie" and net::ERR_INSECURE_RESPONSE in AngularJS拒绝在 AngularJS 中设置不安全的标头“cookie”和 net::ERR_INSECURE_RESPONSE
【发布时间】: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


    【解决方案1】:

    跨域请求被阻止:同源策略不允许读取 https://xx.xx.xx.xx:8443/xxxxxxxx/api/code 的远程资源。 (原因:CORS 标头 'Access-Control-Allow-Origin' 不匹配 '*')。

    您的服务器上似乎没有启用跨域资源共享 (CORS) 或配置错误。

    关于标题问题,你应该看看那个帖子:

    AJAX post error : Refused to set unsafe header "Connection"

    XMLHttpRequest 不允许设置这些标头,它们正在被设置 由浏览器自动生成。原因是通过操纵这些 您可能能够欺骗服务器接受第二个标头 请求通过相同的连接,一个不会通过 通常的安全检查 - 这将是一个安全漏洞 浏览器。

    如果您使用 cookie,则 cookie 应随每个请求自动发送,尤其是在您设置 withCredentials = true 时。

    但是,如果您使用令牌,则必须将它们添加到 Authorization 标头中,通常格式为:“Bearer” + mytoken

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 2018-01-17
      • 2015-03-24
      • 1970-01-01
      • 2016-01-13
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多