允许从任何地方(Java和ember.js)访问-控制-允许-源



我在服务器端使用Java Jersey和Jetty,并有以下一段代码:

    responseBuilder.header("Access-Control-Allow-Origin", "http://localhost:4200");
    responseBuilder.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, auth-token");
    responseBuilder.header("Access-Control-Allow-Credentials", "true");
    responseBuilder.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
    responseBuilder.allow("OPTIONS");

我在我的客户端使用 ember.js 并有以下代码:

/

app/adapters/application.js:

import DS from 'ember-data';
export default DS.RESTAdapter.extend({
    host: 'http://127.0.0.1:20000',
    ajax(url, method, hash) {
        hash = hash || {};
        hash.crossDomain = true;
        hash.xhrFields = {
            withCredentials: true
        };
        return this._super(url, method, hash);
    }
});

代码的组合工作是,它将 COOKIE 作为请求的一部分发送并解决访问控制允许源问题。

但是,我担心的是"http://localhost:4200"是硬编码的。虽然在部署之前这不是问题,但我想这只会限制来自 http://localhost:4200 的流量?这是一个Web应用程序,显然我需要允许来自任何地方的任何客户端的访问。我需要对代码进行哪些更改?

显然我需要允许来自任何地方的任何客户端的访问

我认为这里有一个误解。 Access-Control-Allow-Origin指定客户端应用程序的服务器

客户端应用程序是否在特定源上运行?

  • 如果是:没有问题。为应用程序定义一些profiles。通过这样做,您可以在生产配置文件中定义客户端应用的服务器源
  • 如果否:您可以使用 "*" 来接受 cors 过滤器中的所有源。如果这段代码是由您编写的,只需参数化第二个参数即可给出客户端的主机名。它应该是这样的:request.getRemoteHost();.

最新更新