PHP中文网2017-04-18 09:09:25
可以考慮使用cors-filter
CORS Filter is a universal solution for fitting Cross-Origin Resource Sharing (CORS) support to Java web applications. CORS is a recent W3C effort to introduce a standard melications. CORS is a recent W3C effort to introduce a standard mecalism for enabling sold fal片區 5,5000 00m 筆
具體實作可以參考CORS 跨域 實現想法及相關解決方案,裡面詳細說了CORS和JSONP實現的對比,以及web.xml中CORS的具體配置
怪我咯2017-04-18 09:09:25
springmvc 4.2開始新增對cors的支持,推薦一篇blog :http://blog.csdn.net/isea533/article/details/50449907
如果走nginx 也可以在nginx配置
如下:
server {
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
# add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 200;
}
}
但是這樣做會有個問題 高並發會影響效率
阿神2017-04-18 09:09:25
很普遍的開發需求,這種問題不應該來這裡問,搜尋spring cors文檔 , http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html