浏览器跨域---springboot后台项目跨域问题解决

it2022-12-29  64

配置

// An highlighted block @Configuration public class WebFilterConfig { @Bean public Filter corsFilter() { return new CorsFilter(); } }

跨域过滤器

@Component public class CorsFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH"); response.setHeader("Access-Control-Allow-Headers", "Authorization,Content-Type,X-Requested-With,version,gomanager,token,formtoken,source,loginToken"); response.setHeader("Access-Control-Max-Age", "3600"); if ("OPTIONS".equalsIgnoreCase(httpRequest.getMethod())) { response.setStatus(HttpServletResponse.SC_OK); } else { filterChain.doFilter(httpRequest, response); } } @Override public void destroy() { } }
最新回复(0)