spring cloud 跨域问题 在 zuul 使用 ZuulFilter 解决

在zuul的解决方式

代码如下:

@Component
public class MyFilter extends ZuulFilter {
    public String filterType() {
        return "pre";
    }
	
    public int filterOrder() {
        return 0;
    }
	
    public boolean shouldFilter() {
        return true;
    }
	
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        
        // 跨域
        HttpServletResponse response = ctx.getResponse();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");      
        
        return null;
    }
}

 

发表评论