OPTIONS请求后台处理

Posted itplay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OPTIONS请求后台处理相关的知识,希望对你有一定的参考价值。

package com.ucar.filter;

import cn.hutool.http.Method;
import org.springframework.web.filter.OncePerRequestFilter;

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

/**
 * @author Administrator
 */
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods",
                "POST, GET, OPTIONS, DELETE");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers",
                "Content-Type, x-requested-with, X-Custom-Header, Request-Ajax");//允许自定义的请求头
        if(httpServletRequest.getMethod().toUpperCase().equals(Method.OPTIONS.name())){
            return;
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }


}

  

 

以上是关于OPTIONS请求后台处理的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud+ZUUL跨域请求中的OPTIONS请求处理

跨域访问接口上传图片出现options请求问题解决方法

为啥我用ajax请求后台会请求两次,一次get,一次OPTIONS,求解决

Http option 请求是怎么回事

jqueryeasyuidatagriddata-options的url怎样获得后台java数据

Laravel 对 OPTIONS 请求的响应 405