JAVA解决前端跨域问题。

2021-07-03 23:03

阅读:667

标签:之间   head   control   row   time   The   exce   现在   默认   

什么是跨域?

  通俗来说,跨域按照我自己的想法来理解,是不同的域名之间的访问,就是跨域。不同浏览器,在对js文件进行解析是不同的,浏览器会默认阻止,所以

现在我来说下用java代码解决前端跨域问题。

用java代码解决前端跨域问题?

找到WEB-INF下面的web.xml文件,输入下面代码,在web.xml文件下面:

 1 
 2      3         cors 4         class>com.mj.yiCardCountry.filter.controller.SimpleCORSFilterclass>
 5      6      7         cors 8         /* 9     10     11         1012     

然后创建一个类,在service或者controller  ,我是在controller中创建的,输入以下代码:

 1 public class SimpleCORSFilter implements Filter{
 2 
 3     public void init(FilterConfig filterConfig) throws ServletException {}
 4 
 5     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
 6         HttpServletResponse response = (HttpServletResponse) res;
 7         response.setHeader("Access-Control-Allow-Origin", "*");
 8         response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
 9         response.setHeader("Access-Control-Max-Age", "3628800");
10         response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
11         chain.doFilter(req, res);
12     }
13 
14     public void destroy() {}
15 
16 }

然后,重新install下  重新运行下 试试。基本上是ok了。

 

JAVA解决前端跨域问题。

标签:之间   head   control   row   time   The   exce   现在   默认   

原文地址:https://www.cnblogs.com/lxwt/p/9618367.html


评论


亲,登录后才可以留言!