zoukankan      html  css  js  c++  java
  • SpringMvc服务端实现跨域请求解决方案

    我当时使用的是SimpleCORS,在网上找了一些资料,就开始配置了

    一、服务端

    1、新建过滤器SimpleCORSFilter

    public class SimpleCORSFilter implements Filter {
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
            chain.doFilter(req, res);
        }
        public void init(FilterConfig filterConfig) {}
        public void destroy() {}
    }

    2、在web.xml进行使用

       <filter>
            <filter-name>cors</filter-name>
            <filter-class>com.iotek.api.filter.SimpleCORSFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>cors</filter-name>
          <!-- 开始配置:来允许任何站点对该资源进行跨域请求-->        

         <url-pattern>/*</url-pattern>
        </filter-mapping>


    二、客户端
    在请求头部加入跨域请求的配置信息
  • 相关阅读:
    centos下升级python
    微信公众号(二)
    微信公众号的开发(一)
    nginx 新手入门
    Mysql is not allowed to connect mysql server
    docker 新手入门 (阿里镜像仓库的使用)
    docker 新手入门 (web项目的部署)
    jquery easyui combox不能编辑只能选择
    $.messager.confirm 用法
    html下载excel模板
  • 原文地址:https://www.cnblogs.com/chenandy/p/9444486.html
Copyright © 2011-2022 走看看