码迷,mamicode.com
首页 > 其他好文 > 详细

CORS问题的处理策略

时间:2020-06-24 17:47:18      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:处理   mamicode   现象   utils   bee   ade   tps   work   scan   

  1、问题现象

   Access to XMLHttpRequest at ‘http://yyyy‘ from origin ‘http://zzzz‘ has been blocked by CORS policy: Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response.

       技术图片

  在上图中“Provisional headers are shown”是指显示了临时的请求头,这种现象的产生是当浏览器第一次发送这个请求时请求被阻塞,并未收到响应;而浏览器再次发送这个请求时,由于上一个同样的请求都还没有收到响应,浏览器就会报这个警告。总之来说,就是请求并没有发送出去。

  2、问题分析

  

  3、问题处理

 

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;

@Component
@ServletComponentScan
@WebFilter(urlPatterns = "/*", filterName = "httpFilter")
public class HttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}

 

CORS问题的处理策略

标签:处理   mamicode   现象   utils   bee   ade   tps   work   scan   

原文地址:https://www.cnblogs.com/bien94/p/13188699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!