zoukankan      html  css  js  c++  java
  • JAVA解决前端跨域问题。

    什么是跨域?

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

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

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

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

     1 <!-- 解决跨域访问的问题 -->
     2     <filter>
     3         <filter-name>cors</filter-name>
     4         <filter-class>com.mj.yiCardCountry.filter.controller.SimpleCORSFilter</filter-class>
     5     </filter>
     6     <filter-mapping>
     7         <filter-name>cors</filter-name>
     8         <url-pattern>/*</url-pattern>
     9     </filter-mapping>
    10     <session-config>
    11         <session-timeout>10</session-timeout>
    12     </session-config>

    然后创建一个类,在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了。

  • 相关阅读:
    超级强大的SVG SMIL animation动画详解
    Java Math的 floor,round和ceil的总结
    理解SVG的viewport,viewBox,preserveAspectRatio
    SVG-1
    PHP通过exec执行git pull
    Nodejs中request出现ESOCKETTIMEDOUT解决方案
    Linux安装了mysql 无法远程连接
    linux下docker启动nginx无法访问80端口
    ZPL文件打印
    k8s结合helm部署
  • 原文地址:https://www.cnblogs.com/lxwt/p/9618367.html
Copyright © 2011-2022 走看看