2016-07-26 11 views
0

WildflyサーバーでRESTリクエストを作成しようとしています。まず、古いアプリケーションにログインしてクッキーを取得する必要があります。そのCookieは、REST要求を行う新しいアプリケーション で使用する必要があります。新しいアプリケーションは、実際の既存のものからいくつかのテンプレートです。私は、getAllEntities関数で示されるように、プロパティ、Access-Control-Allow-Credentials、token、crossDomainをプロパティの設定のようなRESTリクエストのヘッダにいくつかのオプションを試しました。AngularJSで安全でないヘッダー "cookie"とnet :: ERR_INSECURE_RESPONSEの設定を拒否しました

RESTリクエストがテストされ、以下のようにFirefoxブラウザのRestClientで正常に動作しました。

私はどのように知りません。

  • は、このことは、これらの2つのエラーを解決するため、私は、以前のアプリケーションから

  • 削除トークンを取得

    • 挿入クッキー(これが不可能であると思われます)書かれているタイトル

    これはですどのようにリクエストが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; 
         } 
        } 
    
    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のに警告を得る:Firebugので

    リクエスト・ヘッダーを:Firebugので0私が手

    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...} 
    

    クロームでは、

    Refused to set unsafe header "cookie" 
    

    net::ERR_INSECURE_RESPONSE 
    
  • 答えて

    1

    クロスオリジンリクエストはブロックされた:同一生成元ポリシーはhttps://xx.xx.xx.xx:8443/xxxxxxxx/api/codeで リモートリソースを読ん禁止します。 (理由:CORSヘッダー 'Access-Control-Allow-Origin'が '*'と一致しません)。

    あなたのサーバーでCross Origin Resource Sharing(CORS)を有効にしていないか、間違って設定されているようです。

    ヘッダの問題について、あなたはその記事を見ている必要がありますが:XMLHttpRequestのは、これらのヘッダを設定することが許可されていない

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

    、彼らは、ブラウザによって自動的に に設定されています。その理由は、これらの ヘッダーを操作すると、同じ接続(2番目の 要求)を受け入れるようにサーバーを騙す可能性があるからです。 通常のセキュリティチェックは行われません。これは ブラウザ。

    クッキーを使用している場合は、特に、withCredentials = trueに設定した場合、各リクエストとともにクッキーが自動的に送信されます。

    ただし、トークンを使用している場合は、一般に「Bearer」+ mytokenの形式でAuthorizationヘッダーに追加する必要があります

    関連する問題