使用HOCON变量在游戏框架中与允许素的CORS问题2.8.15

发布于 2025-02-02 17:40:45 字数 1966 浏览 3 评论 0原文

有一个Java应用程序,该应用程序使用Java,Scala,PlayFramework和Maven。

jdk 11.0.14
play2 version 2.8.15
scala.version 2.12.15
play2.plugin.version 1.0.0-rc6-SNAPSHOT
sbt-compiler.plugin.version   1.0.0

application.conf

play.application.loader = "loader.BasicApplicationLoader"

play.modules.disabled += "play.core.ObjectMapperModule"
play.modules.enabled += "modules.WebObjectMapperModule"

play.modules.enabled += "modules.StartUpModule"
play.modules.enabled += "modules.ClusterMonitoringModule"
play.modules.enabled += "com.kenshoo.play.metrics.PlayModule"

play.filters.enabled = [
    "play.filters.gzip.GzipFilter",
    "com.kenshoo.play.metrics.MetricsFilter",
    "http.filters.ClusterStatusFilter",
    "play.filters.cors.CORSFilter"
]

play.http.actionCreator = "http.BasicActionCreator"
play.http.requestHandler = "http.WebHttpRequestHandler"
play.http.errorHandler = "http.BasicErrorHandler"

server1.host="http://localhost:9000/"
server2.host="http://localhost:2000/"
server3.host="http://localhost:11000/"

play.filters.cors {
     pathPrefixes = ["/"]
     allowedOrigins = [${?server1.host}, ${?server2.host}, ${?server3.host}]
     allowedHttpMethods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
     allowedHttpHeaders = ["Accept", "Origin", "Content-Type"]
     exposedHeaders = ["X-TOTAL-PAGE-COUNT", "X-TOTAL-ELEMENT-COUNT"]
     preflightMaxAge = 1 hour
}

response.delay.milliseconds=500

根据播放文档> ,我发现CORS错误:

'https:// localhost:8080'从Origin'https:// localhost:9000'访问xmlhttprequest:no'access-control-allow-allow-Origin'标头,否则已阻止了'https:// localhost:9000'资源。

到目前为止,我尝试了什么?

我已经检查了服务器,如果正确解释了所使用的变量,则已经部署了该服务器,是的。我需要使用这样的变量,因为根据所需的环境,该URL的值在部署时间在服务器上超过了。

试图添加裸露的访问者访问控制者 - 允许原子,试图将其添加到允许的httpheaders中,而没有运气。

There is a java application, which use java, scala, playframework and maven.

jdk 11.0.14
play2 version 2.8.15
scala.version 2.12.15
play2.plugin.version 1.0.0-rc6-SNAPSHOT
sbt-compiler.plugin.version   1.0.0

application.conf

play.application.loader = "loader.BasicApplicationLoader"

play.modules.disabled += "play.core.ObjectMapperModule"
play.modules.enabled += "modules.WebObjectMapperModule"

play.modules.enabled += "modules.StartUpModule"
play.modules.enabled += "modules.ClusterMonitoringModule"
play.modules.enabled += "com.kenshoo.play.metrics.PlayModule"

play.filters.enabled = [
    "play.filters.gzip.GzipFilter",
    "com.kenshoo.play.metrics.MetricsFilter",
    "http.filters.ClusterStatusFilter",
    "play.filters.cors.CORSFilter"
]

play.http.actionCreator = "http.BasicActionCreator"
play.http.requestHandler = "http.WebHttpRequestHandler"
play.http.errorHandler = "http.BasicErrorHandler"

server1.host="http://localhost:9000/"
server2.host="http://localhost:2000/"
server3.host="http://localhost:11000/"

play.filters.cors {
     pathPrefixes = ["/"]
     allowedOrigins = [${?server1.host}, ${?server2.host}, ${?server3.host}]
     allowedHttpMethods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
     allowedHttpHeaders = ["Accept", "Origin", "Content-Type"]
     exposedHeaders = ["X-TOTAL-PAGE-COUNT", "X-TOTAL-ELEMENT-COUNT"]
     preflightMaxAge = 1 hour
}

response.delay.milliseconds=500

According to the Play documentation this should work, but it does not, i get the cors error:

Access to XMLHttpRequest at 'https://localhost:8080' from origin 'https://localhost:9000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

What I have tried so far?

I have checked on the server, where I have deployed if the variables used are correctly interpreted, and yes, they are. I need to use variables like this, because the values of this urls are overriden on the server at deploy time according to the environment needed.

Tried to add in the exposedHeader the Access-Control-Allow-Origin, tried to add it in the allowedHttpHeaders and no luck..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2025-02-09 17:40:45

因此,在发布了这个问题之后,我弄清楚了这个问题。

问题是那些服务器值:

server1.host="http://localhost:9000/"
server2.host="http://localhost:2000/"
server3.host="http://localhost:11000/" especially the slash (/) at the end.

因为浏览器为Origin“ http:// localhost:9000”而无需斜线,这就是为什么差异的原因。

So, after posting this questions I have figured out the question.

The issue were those server values:

server1.host="http://localhost:9000/"
server2.host="http://localhost:2000/"
server3.host="http://localhost:11000/" especially the slash (/) at the end.

For the browser was origin "http://localhost:9000" without slash and that is why the difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文