其他分享
首页 > 其他分享> > spring boot处理跨域请求代码

spring boot处理跨域请求代码

作者:互联网

@Configuration
@WebFilter(filterName = "CorsFilte")
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        filterChain.doFilter(servletRequest, response);
    }
}

标签:Control,跨域,spring,boot,HttpServletResponse,Access,Allow,response,setHeader
来源: https://www.cnblogs.com/xiaomitu/p/15214084.html