• spring boot工程前端ajax请求报Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight respon的解决办法


    办法:

    在后台设置过滤器


    import org.springframework.stereotype.Component;

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

    @Component
    public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    HttpServletResponse response = (HttpServletResponse) servletResponse;
    HttpServletRequest request = (HttpServletRequest)servletRequest;

    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", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,x-requested-with,Authorization");
    response.setHeader("Access-Control-Allow-Credentials", "true");
    String method = request.getMethod();
    if(method.equalsIgnoreCase("OPTIONS")){
    servletResponse.getOutputStream().write("Success".getBytes("utf-8"));
    }else{
    filterChain.doFilter(servletRequest, servletResponse);
    }
    }

    @Override
    public void destroy() {

    }

    }
  • 相关阅读:
    7段数码管绘制
    画五角星
    绘制正方形
    蟒蛇的绘制
    玫瑰花
    小猪佩奇
    数列求和
    水仙花数
    鸡兔同笼
    画国际象棋盘
  • 原文地址:https://www.cnblogs.com/superman-21/p/13773517.html
Copyright © 2020-2023  润新知