我们有两个用于UI和服务器的项目,两个项目都在不同的域上运行。对于UI,我们使用角度JS,而对于服务器端,我们使用spring并向客户端提供JSON响应。由于CORS问题,我们无法与服务器通信。我们用Google搜索并尝试了不同的解决方案。我们找到了以下链接。
https://htet101.wordpress.com/2014/01/22/cors-with-angularjs-and-spring-rest/
我们完成了链接中提到的任何内容。它适用于GET请求,但它不适用于POST请求。当我们执行POST请求时,我们收到400 Bad Request。请查看以下详细信息以获取更多信息。
响应标头:HTTP / 1.1 400错误请求服务器:Apache-Coyote / 1.1 Access-Control-Allow-Origin:* Access-Control-Allow-Methods:POST, 获取,选项,删除Access-Control-Max-Age:3600 Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type, 接受Content-Type:text / html; charset = utf-8 Content-Language:en 内容长度:1113日期:星期一,28九月2015 09:33:21 GMT连接: 靠近
请求标头:POST / controller / addLogin HTTP / 1.1主机: 192.168.5.131:8080连接:keep-alive内容长度:41 Pragma:no-cache Cache-Control:no-cache接受:application / json,text / plain, /来源:localhost:1337用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 45.0.2454.101 Safari / 537.36内容类型: application / json; charset = UTF-8 Referer: localhost:1337 / login.html Accept-Encoding:gzip,deflate Accept-Language:en-US,en; q = 0.8
在得到这个后,我们在客户端添加了以下行,正如我们在stacloverflow中的一些解决方案中看到的那样。但是错误仍然是一样的。
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.headers.common['Access-Control-Allow-Credentials'] = true;
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.post['Content-type'] = "application/json";
请帮助我们解决这个问题。
更新的问题:
我们做了以下答案中提到的确切更改。然后我们得到了像
这样的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'corsFilter' is defined for enabling cors.
为了解决这个问题,我们创建了bean
<bean id="corsFilter" class="com.prototype.helper.SimpleCORSFilter" />
。但是对我们来说仍然没有运气,我们仍然有400个不好的请求,比如
POST http://IP地址:8080 / controller / addLogin 400(错误请求)。
以下是我们所做的过滤器类和web.xml配置:
public class SimpleCORSFilter extends OncePerRequestFilter {
// private Set<String> whitelist = Sets.newHashSet("[AllowedOrigin1]", "[AllowedOrigin2]");
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String originHeaderFromClient = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", originHeaderFromClient);
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type");
filterChain.doFilter(request, response);
}
}
的web.xml:
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 0 :(得分:1)
如果您在浏览器的开发人员选项中检查网络选项卡,您可能会看到浏览器在进行POST调用之前正在对服务器进行OPTIONS调用。
Spring的DispatcherServlet默认阻止OPTIONS调用进入您的应用程序。意思是,OPTIONS调用不会通过。您的浏览器认为您尝试访问的服务器不支持CORS,因此会阻止呼叫通过。
我们实现了一个过滤器,并在web.xml中使用Traceback (most recent call last):
File "/home/ec2-user/DynamoDB_script.py", line 29, in <module>
'daysabsent':'30'
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 821, in put_item
return item.save(overwrite=overwrite)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/items.py", line 455, in save
returned = self.table._put_item(final_data, expects=expects)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 835, in _put_item
self.connection.put_item(self.table_name, item_data, **kwargs)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
body=json.dumps(params))
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
retry_handler=self._retry_handler)
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 954, in _mexe
status = retry_handler(response, i, next_sleep)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2882, in _retry_handler
response.status, response.reason, data)
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
{u'message': u'One or more parameter values were invalid: Type mismatch for key version expected: N actual: S', u'__type': u'com.amazon.coral.validate#ValidationException'}
连接它,如下所示:
DelegatingFilterProxy
在过滤器的<filter>
<filter-name>corsFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方法中,执行了以下操作:
doFilter
这样,无论GET / POST如何,所有调用都将获得 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest) servletRequest;
String originHeaderFromClient = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", <<acceptedOrigins>>;
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type");
chain.doFilter(request, response);
标头。并且它还使控制器保持清洁CORS头特定设置(如果你有多个控制器是痛苦的话)。