美文网首页
后端Java跨域

后端Java跨域

作者: 不要变成发抖的小喵喵喵喵喵喵 | 来源:发表于2017-08-16 10:30 被阅读0次

后端Java
第一步:CORSFilter.java文件 过滤器

package com.seed.cayenne.console.web.app.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lldzy on 2017/06/27.
 */
public class CORSFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }
    
}

第二步:webapp > WEB-INFO > web.xml

<!-- 解决跨域请求的filter -->
    <filter>
        <filter-name>corsFilter</filter-name>
        <filter-class>com.superyz.jcode.filter.CORSFilter</filter-class>   //路径要改为上面Java文件的路径
    </filter>
    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

相关文章

网友评论

      本文标题:后端Java跨域

      本文链接:https://www.haomeiwen.com/subject/xmzbrxtx.html