2016-11-21 106 views
1

我想加为我的阿卡HTTP API CORS支持:https://github.com/lomigmegard/akka-http-corsCORS与headerValueByName问题(阿卡-HTTP-CORS)阿卡HTTP同时使用<code>akka-http-cors</code>

一切正常时,我基本上将CORS支持一个简单路线,例如:

val route = cors() { 
    path("ping") { 
     get { 
      complete("pong") 
     } 
    } 
} 

与相应的jQuery的电话:

$.ajax({ 
    url: "http://localhost:9000/ping", 
    type: "GET", 
    success: function(data) { alert(data); } 
    }); 

返回正确“傍”预期


但是,当我尝试提取(服务器端)从请求某些特定标题时,CORS支持的响应似乎是突然被打破。例如,有:

val route = cors() { 
    headerValueByName("myheader") { (myheader) => 
     path("ping") { 
      get { 
       complete("pong") 
      } 
     } 
    } 
} 

与相应的jQuery的电话:

$.ajax({ 
    url: "http://localhost:9000/ping", 
    type: "GET", 
    beforeSend: function(xhr){xhr.setRequestHeader('myheader', 'test');}, 
    success: function(data) { alert('Success!' + data); } 
    }); 

失败,CORS错误控制台:

XMLHttpRequest cannot load http://localhost:9000/ping. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8080' is therefore not allowed access. 
The response had HTTP status code 400. 

看来,加入headerValueByName(...)到航路中断cors支持,我不明白为什么。

我也尝试过cors的不同实现(基于自定义特性),并且所有这些行为都是相同的。

我在这里错过了什么?

回答

1

请使用类似curl这样的工具来调试您的服务器路由,以查看来自服务器的实际响应,而不是JavaScript的解释。

curl -X GET -H "Origin: http://example.com" -H "myheader: test" http://localhost:9000/ping 

我怀疑你的自定义头没有在HTTP请求中正确发送。 headerValueByName指令将拒绝该请求。拒绝冒泡(跳过cors指令)最终由默认拒绝处理程序处理。 CORS相关的标题因此没有响应。

你应该有你的拒绝和异常处理程序里面cors指令,而不是外部(如默认的)。请看下面的例子。

def route: Route = { 
    import CorsDirectives._ 
    import Directives._ 

    // Your CORS settings 
    val corsSettings = CorsSettings.defaultSettings 

    // Your rejection handler 
    val rejectionHandler = corsRejectionHandler withFallback RejectionHandler.default 

    // Your exception handler 
    val exceptionHandler = ExceptionHandler { 
    ... 
    } 

    // Combining the two handlers only for convenience 
    val handleErrors = handleRejections(rejectionHandler) & handleExceptions(exceptionHandler) 

    // Note how rejections and exceptions are handled *before* the CORS directive (in the inner route). 
    // This is required to have the correct CORS headers in the response even when an error occurs. 
    handleErrors { 
    cors(corsSettings) { 
     handleErrors { 
     ... // your business route here 
     } 
    } 
    } 
} 

这不会解决你的头的问题,但至少CORS标头将是,即使该路由被拒绝或失败,一个异常的HTTP响应的一部分。

+0

非常感谢!你完全正确:) – Elsener