我终于得到了 apache Tomcat8 的答案
您必须编辑 tomcat web.xml 文件。
可能它会在 webapps 文件夹中,
sudo gedit /opt/tomcat/webapps/your_directory/WEB-INF/web.xml
找到它并编辑它
<web-app>
<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>
这将允许 Access-Control-Allow-Origin 所有主机。如果您需要将其从所有主机更改为仅您的主机,您可以编辑
<param-name>cors.allowed.origins</param-name>
<param-value>http://localhost:3000</param-value>
以上代码从 * 到您的http://your_public_IP或http://www.example.com
你可以参考这里Tomcat过滤器文档
谢谢