2016-08-17 6 views
1

私のVertx ServerはサーバーAにあり、クライアントはサーバーBにあります。私がvertxサーバーにアクセスしようとすると、CORSエラーが表示されます。CORS問題を処理するサーバー側のコードを追加しました。ワーキング。クライアント側にヘッダを追加する必要がありますか?私はここで何が欠けていますか?私はheader.Now私のコードでwithCredential属性がvertexでのCORSの問題アプリケーションが動作しない

のように見える削除

Router router = Router.router(vertx); 
router.route().handler(BodyHandler.create()); 
router.route().handler(io.vertx.rxjava.ext.web.handler.CorsHandler.create("*") 
.allowedMethod(io.vertx.core.http.HttpMethod.GET) 
.allowedMethod(io.vertx.core.http.HttpMethod.POST) 
.allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS) 
.allowedHeader("Access-Control-Request-Method") 
.allowedHeader("Access-Control-Allow-Credentials") 
.allowedHeader("Access-Control-Allow-Origin") 
.allowedHeader("Access-Control-Allow-Headers") 
.allowedHeader("Content-Type")); 

クライアントの実装:

function(url, user) { 
    eventBus = new EventBus(url); 
    eventBus.onopen = function() { 
      //Do Something 
    } 
} 

を更新誰もが

VERTXサーバーサイドのを助けることができます

if (ar.succeeded()) { 
routingContext.response().setStatusCode(200).setStatusMessage("OK") 
.putHeader("content-type", "application/json") 
.putHeader("Access-Control-Allow-Origin", "*") 
.putHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS") 
.end(
Json.encodePrettily(ar.result().body()) 
//(String) ar.result().body() 
); 
routingContext.response().close(); 

まだ次のエラーが表示されます。手伝ってくれますか?

XMLHttpRequest cannot load https://192.168.1.20:7070/Notify/571/rn4nh0r4/xhr_send?t=1471592391921. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'https://login.com' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute. 

アップデート2:

.putHeader("Access-Control-Allow-Origin", "*") 

で私のクライアントのアドレスを追加した後、私は次しまったログ:

XMLHttpRequest cannot LOAD https://192.168.1.20:7070/Notify/773/k3zq1z2z/xhr_send?t=1471601521206. Credentials flag IS 'true', but the 'Access-Control-Allow-Credentials' header IS ''. It must be 'true' TO allow credentials. Origin 'https://login.com' IS therefore NOT allowed access. 

次のように私のコードは次のとおりです。

if (ar.succeeded()) { 
routingContext.response().setStatusCode(200).setStatusMessage("OK") 
.putHeader("content-type", "application/json") 
.putHeader("Access-Control-Allow-Origin", "https://login.com") 
.putHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS") 
.putHeader("Access-Control-Allow-Credentials", "true") 
.end(
Json.encodePrettily(ar.result().body()) 
//(String) ar.result().body() 
); 
routingContext.response().close(); 
+1

に設定されたあなたはhttp://stackoverflow.com/questions/33124625/how-to-を見たことがありますが、次のように私のコードがありますx-2-x質問を有効にする? –

+0

@カロール私は試しましたが、エラーを投げます。 失敗:WebSocketハンドシェイク中にエラーが発生しました:予期しない応答コード:400 –

+0

WebSocketはこのHTTP要求と関係がありますか? :) –

答えて

3

この問題は、ルータハンドラ内でwithCredentialフラグをtrueに追加することで解決しました。

router.route().handler(io.vertx.rxjava.ext.web.handler.CorsHandler.create("https://login.com") 
.allowedMethod(io.vertx.core.http.HttpMethod.GET) 
.allowedMethod(io.vertx.core.http.HttpMethod.POST) 
.allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS) 
.allowCredentials(true) 
.allowedHeader("Access-Control-Allow-Method") 
.allowedHeader("Access-Control-Allow-Origin") 
.allowedHeader("Access-Control-Allow-Credentials") 
.allowedHeader("Content-Type")); 

以前にそれが唯一のレスポンスヘッダ

if (ar.succeeded()) { 
routingContext.response().setStatusCode(200).setStatusMessage("OK") 
.putHeader("content-type", "application/json") 
.putHeader("Access-Control-Allow-Origin", "https://login.com") 
.putHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS") 
.putHeader("Access-Control-Allow-Credentials", "true") 
.end(
Json.encodePrettily(ar.result().body()) 
//(String) ar.result().body() 
); 
routingContext.response().close(); 
2

これは、ルートを定義する際に順序が関係するからです。 は、単にあなたのCORSとBodyHandler切り替える:

Router router = Router.router(vertx); 
router.route().handler(io.vertx.rxjava.ext.web.handler.CorsHandler.create("*") 
.allowedMethod(io.vertx.core.http.HttpMethod.GET) 
.allowedMethod(io.vertx.core.http.HttpMethod.POST) 
.allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS) 
.allowedHeader("Access-Control-Request-Method") 
.allowedHeader("Access-Control-Allow-Credentials") 
.allowedHeader("Access-Control-Allow-Origin") 
.allowedHeader("Access-Control-Allow-Headers") 
.allowedHeader("Content-Type")); 
router.route().handler(BodyHandler.create()); 
+1

サーバーで「Access-Control-Allow-Credentials」を設定した場合、クライアントのwithCredentialsを削除しないでください。 http://stackoverflow.com/questions/24687313/what-exactly-does-the-access-control-allow-credentials-header-do –

+0

あなたの提案は助け&私は私の答えを掲載しました。 –

1

あなたのクライアントがサーバーに接続する場合は、単に

.putHeader("Access-Control-Allow-Origin", "*") 

(の代わりに、*)で、あなたのクライアントのアドレスを追加し

したがって、あなたがなりますお使いのブラウザからwithCredentialsリクエストを送信することができます。

+0

私はあなたの提案を実装し、それに従って質問を更新しました。見てください。 –

関連する問題