• java解决跨域问题


    Step1 添加SimpleCORSFilter过滤器类

    package tools;

    import java.io.IOException;

    import javax.servlet.Filter;

    import javax.servlet.FilterChain;

    import javax.servlet.FilterConfig;

    import javax.servlet.ServletException;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServletResponse;

    public class SimpleCORSFilter implements Filter {

        @Override

        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

            HttpServletResponse res = (HttpServletResponse)response;

            res.setHeader("Access-Control-Allow-Origin", "*");

            res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

            res.setHeader("Access-Control-Max-Age", "3600");

            res.setHeader("Access-Control-Allow-Headers", "x-requested-with");

            chain.doFilter(request, response);

        }

        @Override

        public void destroy() {

        }

    }

    Step2 在web.xml中配置过滤器

    <filter>

            <filter-name>cors</filter-name>

            <filter-class>tools.SimpleCORSFilter</filter-class>

        </filter>

        <filter-mapping>

            <filter-name>cors</filter-name>

            <url-pattern>/*</url-pattern>

        </filter-mapping>

  • 相关阅读:
    bin/sh^M: bad interpreter: No such file or directory解决
    1.2前置条件
    LINUXIPCS信息
    动态添加样式表规则第3版
    Google放出C++代码风格规范
    我的模块加载系统 v4
    有关婚姻的名言
    javascript 测试工具abut v3
    全世界最短的domReady
    javascript 模板系统 ejs v7
  • 原文地址:https://www.cnblogs.com/rangewr/p/10710662.html
Copyright © 2020-2023  润新知