访问docker容器内的Tomcat时出现CORS错误

时间:2017-10-07 11:36:10

标签: tomcat docker cors camunda

我正在构建一个Angular 4应用程序,它使用REST API与Camunda进程交互。尝试登录,面临CORS错误。

Dockerfile:

FROM camunda/camunda-bpm-platform:tomcat-latest

ADD sample.war /camunda/webapps/sample.war

COPY ./web.xml /camunda/webapps/engine-rest/WEB-INF

像以下一样运行:

docker build -t camunda-eval .
docker run -d --name camunda -p 8080:8080 <image>

控制台出错:

  

无法加载   http://localhost:8080/camunda/api/admin/auth/user/default/login/tasklist:   请求中不存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:4200”   访问。

我的web.xml如下所示:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <filter>
    <filter-name>EmptyBodyFilter</filter-name>
    <filter-class>org.camunda.bpm.engine.rest.filter.EmptyBodyFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>EmptyBodyFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>CacheControlFilter</filter-name>
    <filter-class>org.camunda.bpm.engine.rest.filter.CacheControlFilter</filter-class>    
  </filter>
  <filter-mapping>
    <filter-name>CacheControlFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- Http Basic Authentication Filter -->
  <!-- <filter>
    <filter-name>camunda-auth</filter-name>
    <filter-class>
      org.camunda.bpm.engine.rest.security.auth.ProcessEngineAuthenticationFilter
    </filter-class>
    <init-param>
      <param-name>authentication-provider</param-name>
      <param-value>org.camunda.bpm.engine.rest.security.auth.impl.HttpBasicAuthenticationProvider</param-value>
    </init-param>
    <init-param>
        <param-name>rest-url-pattern-prefix</param-name>
        <param-value></param-value>
      </init-param> 
  </filter>

  <filter-mapping>
    <filter-name>camunda-auth</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> -->

  <filter>
      <filter-name>Resteasy</filter-name>
      <filter-class>
          org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
      </filter-class>
      <init-param>
          <param-name>javax.ws.rs.Application</param-name>
          <param-value>org.camunda.bpm.engine.rest.impl.application.DefaultApplication</param-value>
      </init-param>
  </filter>

  <filter-mapping>
      <filter-name>Resteasy</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 <filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
    <param-name>cors.exposed.headers</param-name>
    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
  </init-param>
  <init-param>
    <param-name>cors.support.credentials</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>cors.preflight.maxage</param-name>
    <param-value>10</param-value>
  </init-param>
</filter>
  <filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:3)

您收到此错误是因为“localhost:4200”和“localhost:8080”与CORS不同。

一般来说,您有3种方法可以避免这种情况:

  1. 告诉您的浏览器忽略CORS(例如chromium-browser --disable-web-security --user-data-dir) - 如果您只想玩游戏,这是最快的,但当然没有生产解决方案
  2. 在8080上设置CORS标头以允许从4200进行访问...此处不适用(很容易,因为您无法控制camunda-app)
  3. 使用“边缘”(或“网关”)服务器,该服务器通过反向代理在内部重新路由请求。含义:您的浏览器只与网关通信(比如端口80),这会将请求路由到您的任务列表(4200)或camunda(8080)。这是解决这个问题的标准方法,如果你寻找nginx或zuul,你会发现很好的文档。但是,它需要一个额外的服务组件。