Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java play应用程序中启用CORS筛选器_Java_Angularjs_Cors_Playframework 2.4 - Fatal编程技术网

在Java play应用程序中启用CORS筛选器

在Java play应用程序中启用CORS筛选器,java,angularjs,cors,playframework-2.4,Java,Angularjs,Cors,Playframework 2.4,我尝试在我的java play应用程序上启用CORS过滤器。我正在使用angularjs访问localhost以获取一些数据。这是我在play应用程序端所做的 application.conf play.http.filters = "com.de.filters.Filters" play.filters.cors { allowedOrigins = null allowedHttpMethods = ["GET", "POST"] allowedHttpHeader

我尝试在我的java play应用程序上启用CORS过滤器。我正在使用angularjs访问localhost以获取一些数据。这是我在play应用程序端所做的

application.conf

play.http.filters = "com.de.filters.Filters"
play.filters.cors {
    allowedOrigins = null
    allowedHttpMethods = ["GET", "POST"]
    allowedHttpHeaders = ["Accept"]
    preflightMaxAge = 3 days
}
Filters.java

public class Filters implements HttpFilters {
    @Inject
    CORSFilter corsFilter;

    public EssentialFilter[] filters() {
        return new EssentialFilter[]{corsFilter};
    }
}
当我打电话给我的服务时,它给了我一个错误

无法加载XMLHttpRequest。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为403

如何修复此问题?

从, 首先将过滤器添加到
build.sbt
中的
libraryDependencies
列表中,如下所述

libraryDependencies += filters
然后,在
application.conf
中,不要将
play.filters.cors.allowedOrigins
设置为
[null]
,它默认为“允许所有来源”。设置一个有效的域列表或忽略它。不要忘记引用您的过滤器:

play.http.filters = "filters.Filters"
最后,在您的
Filters.filter()
方法中,编写如下内容,指定
asJava()
方法,如文档中所述

public EssentialFilter[] filters() {
    return new EssentialFilter[] { 
        corsFilter.asJava()
    };
}

将这些行添加到application.conf中

play.filters.cors {
  # allow all paths
  pathPrefixes = ["/"]
  # allow all origins
  allowedOrigins = null
  allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
  # allow all headers
  allowedHttpHeaders = null
}

你可以参考我的答案了解更多信息。

我终于让我的公司过滤器开始工作了。关于这方面的文档非常少,而且其中的内容不起作用。希望这能帮助别人。(重头戏2.5.4)

还将其添加到application.conf中

play.filters.cors{
# allow all paths
  pathPrefixes = ["/"]
  # allow all origins
  allowedOrigins = null
  allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
  # allow all headers
  allowedHttpHeaders = null
}

也许可以尝试将AllowedOriginates设置为空字符串而不是null<代码>允许起源=。或者干脆省略这个参数。看看它是否允许这样做。或者将localhost或127.0.0.1添加到它中。我做了
allowedOrigins=[“127.0.0.1”]
。但我仍然无法访问。Java的2.5过滤器显然已损坏,但已修复:
play.filters.cors{
# allow all paths
  pathPrefixes = ["/"]
  # allow all origins
  allowedOrigins = null
  allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
  # allow all headers
  allowedHttpHeaders = null
}