java - springMVC 怎么实现跨域请求?
黄舟
黄舟 2017-04-18 09:07:25
0
4
674

springMVC 怎么实现跨域请求?配置了下没有实现!

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

répondre à tous(4)
Ty80

可以考虑使用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 mechanism for enabling cross-domain requests in web browsers and participating servers.

具体实现可以参考CORS 跨域 实现思路及相关解决方案,里面详细说了CORS和JSONP实现的对比,以及web.xml中CORS的具体配置

刘奇

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;  
        }  
}  

但是这样做会有个问题 高并发会影响效率

阿神

很普遍的开发需求,这种问题不应该来这里问,搜索spring cors文档 , http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html

阿神

想实现jsonp请求么,看看我的Demo:http://git.oschina.net/gradle/ssmbootstrap_table

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!