CORS 问题/akka-http-cors/Scala/Java 中请求的资源上不存在 'Access-Control-Allow-Origin' header
CORS issue / akka-http-cors / No 'Access-Control-Allow-Origin' header is present on the requested resource in Scala/Java
如何正确实施 akka-http-cors 以允许从任何来源(域,在我的例子中 localhost:3000)访问 Https 服务器(Scala/Akka)?
根据 akka-http-cors 文档应该 defaultSettings 完成这项工作,但我仍然做错了:
...
import ch.megard.akka.http.cors.scaladsl.CorsDirectives._
var settings = CorsSettings.defaultSettings
lazy val theRequestRoutes: Route =
cors(settings) {
pathPrefix("getsearchresult") {
post {
entity(as[Server.ReqGetSearchResult]) { request: ReqGetSearchResult =>
val operationPerformed: Future[Server.ServerResponse] = controller.ask(ControllerActor.Request_GetSearchResult(_, request))
onSuccess(operationPerformed) {
case Server.Send_Response_Success(jsonResponse) => complete(jsonResponse)
case Server.Send_Response_Failure(status) => complete("""{"status":"server_error"}""")
}
}
}
}
}
...
val https: HttpsConnectionContext = getHttpsConnectionContext()
val serverBinding: Future[Http.ServerBinding] = Http().newServerAt(host, port).enableHttps(https).bind(theRequestRoutes)
libraryDependencies += "ch.megard" %% "akka-http-cors" % "1.1.2"
Google Chrome 错误:
Access to XMLHttpRequest at 'https://api.domain.com:8080/getsearchresult' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Firefox 错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.domain.com:8080/getsearchresult. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
akka-http-core: https://github.com/lomigmegard/akka-http-cors#configuration
非常感谢任何帮助!
检查这个要点 URL ~ https://gist.github.com/hakimkal/bd5ca349ef10e8981e121eb865023067
这是我的 Main.scala 文件使用上述特征
val cors = new CORSHandler {}
val route = new MerchantRoutes(merchantService, authService)
val serverBinding = Http()
.newServerAt(host, port)
.bind(
cors.corsHandler(route.merchantRoutes)
)
我的问题是我没有以正确的方式进行编组,因此它在以下行中崩溃:
entity(as[Server.ReqGetSearchResult]) { request: ReqGetSearchResult =>
令我惊讶的是,错误被标记为 CORS 错误。
我通过查看 Akka HTTP JSON 支持手册解决了这个问题:https://doc.akka.io/docs/akka-http/current/common/json-support.html
在我的案例中,通过预先设置它现在对我有用:
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val clientRequestFormat = jsonFormat5(Server.ReqGetSearchResult)
}
class RequestRoutes(controller: ActorRef[Controller.Command])(implicit system: ActorSystem[_]) extends Directives with JsonSupport {
import akka.actor.typed.scaladsl.AskPattern.schedulerFromActorSystem
import akka.actor.typed.scaladsl.AskPattern.Askable
implicit val timeout: Timeout = 60.seconds
... (code from above)
}
如何正确实施 akka-http-cors 以允许从任何来源(域,在我的例子中 localhost:3000)访问 Https 服务器(Scala/Akka)?
根据 akka-http-cors 文档应该 defaultSettings 完成这项工作,但我仍然做错了:
...
import ch.megard.akka.http.cors.scaladsl.CorsDirectives._
var settings = CorsSettings.defaultSettings
lazy val theRequestRoutes: Route =
cors(settings) {
pathPrefix("getsearchresult") {
post {
entity(as[Server.ReqGetSearchResult]) { request: ReqGetSearchResult =>
val operationPerformed: Future[Server.ServerResponse] = controller.ask(ControllerActor.Request_GetSearchResult(_, request))
onSuccess(operationPerformed) {
case Server.Send_Response_Success(jsonResponse) => complete(jsonResponse)
case Server.Send_Response_Failure(status) => complete("""{"status":"server_error"}""")
}
}
}
}
}
...
val https: HttpsConnectionContext = getHttpsConnectionContext()
val serverBinding: Future[Http.ServerBinding] = Http().newServerAt(host, port).enableHttps(https).bind(theRequestRoutes)
libraryDependencies += "ch.megard" %% "akka-http-cors" % "1.1.2"
Google Chrome 错误:
Access to XMLHttpRequest at 'https://api.domain.com:8080/getsearchresult' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Firefox 错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.domain.com:8080/getsearchresult. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
akka-http-core: https://github.com/lomigmegard/akka-http-cors#configuration
非常感谢任何帮助!
检查这个要点 URL ~ https://gist.github.com/hakimkal/bd5ca349ef10e8981e121eb865023067
这是我的 Main.scala 文件使用上述特征
val cors = new CORSHandler {}
val route = new MerchantRoutes(merchantService, authService)
val serverBinding = Http()
.newServerAt(host, port)
.bind(
cors.corsHandler(route.merchantRoutes)
)
我的问题是我没有以正确的方式进行编组,因此它在以下行中崩溃:
entity(as[Server.ReqGetSearchResult]) { request: ReqGetSearchResult =>
令我惊讶的是,错误被标记为 CORS 错误。
我通过查看 Akka HTTP JSON 支持手册解决了这个问题:https://doc.akka.io/docs/akka-http/current/common/json-support.html
在我的案例中,通过预先设置它现在对我有用:
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val clientRequestFormat = jsonFormat5(Server.ReqGetSearchResult)
}
class RequestRoutes(controller: ActorRef[Controller.Command])(implicit system: ActorSystem[_]) extends Directives with JsonSupport {
import akka.actor.typed.scaladsl.AskPattern.schedulerFromActorSystem
import akka.actor.typed.scaladsl.AskPattern.Askable
implicit val timeout: Timeout = 60.seconds
... (code from above)
}