Allow cross domain scripting in mvc web application

夙愿已清 提交于 2020-01-06 20:13:09

问题


I have a web application and im using spring mvc. I need to allow cross domain scripting (i need that a web service that i externalize can be accessed by javascript). I wrote a crossdomain.xml and i put it under the WEB-INF folder. This is my crossdomain.xml

<?xml version="1.0" encoding="UTF-8"?>
<cross-domain-policy>
  <allow-access-from domain="*" />
</cross-domain-policy>

I have searched around and saw that the crossdomain.xml should be accessable from this URL: "localhost:8080/crossdomain.xml"
the thing is that the mvc is rerouting the request to the main controller (with RequestMapping annotation of "/"). How can i overcome this using spring mvc?
Thank you.


回答1:


crossdomain.xml is used only by flash, so that's probably not what you actually want.

If you want cross domain xhr requests done with javascript, you should be using either CORS or JSONP, see this related question

CORS example:

access-control-allow-origin: http://api.example.com
access-control-allow-credentials: true

JSONP example (with jQuery, requires server side support):

$.ajax({
    type: "GET",
    url: "https://domain.com/jsonp-service",
    dataType: "JSONP"
});



回答2:


There is a special integration between jax rs jersey implementation and spring. all explained in this blog Jersey Spring integration



来源:https://stackoverflow.com/questions/11377994/allow-cross-domain-scripting-in-mvc-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!