Liberty 服务器上配置跨源资源共享

可以对 Liberty 服务器上的 Web 应用程序启用跨源资源共享 (CORS)。

关于此任务

启用 CORS 将允许 JavaScript 客户机针对 Liberty 服务器上的应用程序发出请求,即使客户机和服务器在两个不同域上。Web 浏览器会因为同源策略阻止这些请求。

过程

  1. 确保服务器配置具有所部署应用程序需要的所有功能部件,例如,servlet-3.0jaxrs-1.1 等等。还应确保端口和用户注册表设置对于所部署应用程序而言是正确的。
  2. 将 CORS 服务设置添加至 server.xml 文件。cors 元素在 domain 中定义要设置的 URL 的 CORS 设置。

示例

以下是 sampleApp/path 上下文根上 Web 应用程序的 CORS 配置的示例。

<cors domain="/sampleApp/path"
      allowedOrigins="https://alice.com:8090"
      allowedMethods="GET, DELETE, POST"
      allowedHeaders="accept, MyRequestHeader1"
      exposeHeaders="MyResponseHeader1"
      allowCredentials="true"
      maxAge="3600" />

以下是 RESTful 端点 /ibm/api/collective 的 CORS 配置的示例。此设置将适用于以 /ibm/api/collective 开头的所有端点路径(包括 ibm/api/collective/docs)。

<cors domain="/ibm/api/collective"
      allowedOrigins="https://alice.com:8090"
      allowedMethods="GET, DELETE, POST"
      allowedHeaders="accept, MyRequestHeader1"
      exposeHeaders="MyResponseHeader1"
      allowCredentials="true"
      maxAge="3600" />

用于指示主题类型的图标 任务主题

文件名:twlp_webcontainer_cors_config.html