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", httpServletRequest.getHeader("Origin")); httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true"); 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); } }
---跨域 JavaOPTIONS 情况的支持
标签:Control,Access,跨域,httpServletResponse,Filter,import,OPTIONS From: https://www.cnblogs.com/porter/p/16662413.html