Home  >  Q&A  >  body text

java - springMVC 怎么实现跨域请求?

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

黄舟黄舟2742 days ago676

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:09:25

    Consider using 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.

    For specific implementation, please refer to CORS cross-domain implementation ideas and related solutions, which details the comparison between CORS and JSONP implementation, as well as the specific configuration of CORS in web.xml

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:09:25

    springmvc 4.2 has added support for cors. I recommend a blog: http://blog.csdn.net/isea533/article/details/50449907
    If you use nginx, you can also configure it in nginx
    as follows:

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

    But there will be a problem with this. High concurrency will affect efficiency

    reply
    0
  • 阿神

    阿神2017-04-18 09:09:25

    A very common development requirement. This kind of question should not be asked here. Search the spring cors document, http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html

    reply
    0
  • 阿神

    阿神2017-04-18 09:09:25

    If you want to implement jsonp request, take a look at my Demo: http://git.oschina.net/gradle/ssmbootstrap_table

    reply
    0
  • Cancelreply