简体   繁体   English

Tomcat REST API上的CORS访问

[英]CORS Access on Tomcat REST API

I have a local Tomcat installation running on port 8081 exposing a REST API. 我在端口8081上运行本地Tomcat安装,公开REST API。 My development web server is running on port 9000. Now I want to call the REST from javascript code running in the browser using angular's $http . 我的开发Web服务器在端口9000上运行。现在我想使用angular的$http从浏览器中运行的javascript代码调用REST。 This is obviously a CORS call and I had the error: 这显然是一个CORS调用,我有错误:

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:9000 ' is therefore not allowed access." “请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许来源' http:// localhost:9000 '。”

I added a filter to the Tomcat configuration as proposed by the documentation: 我根据文档的建议为Tomcat配置添加了一个过滤器:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

This worked perfectly fine for my GET requests, but now I want to perform POST requests and the error is back again. 这对我的GET请求完全正常,但现在我想执行POST请求,错误又回来了。 Why so? 为什么这样? Shouldn't that filter allow POST as well? 那个过滤器不应该允许POST吗?

You need to add some init params : 你需要添加一些init参数

<init-param>
  <param-name>cors.allowed.methods</param-name>
  <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Last-Modified</param-value>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM