Postwoman跨域问题 has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present
程序员文章站
2022-07-10 15:42:02
...
Postwoman跨域问题 has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present
在使用postwoman过程中用平时访问postman的流程走了一遍,结果出现说
has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
解决方案
加个Filter即可解决
@Order(Ordered.HIGHEST_PRECEDENCE)
@WebFilter(filterName = "crossFilter", urlPatterns = "/*")
@Component
public class CrossFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpServletResponse servletResponse = (HttpServletResponse) response;
servletResponse.setHeader("Access-Control-Allow-Origin", servletRequest.getHeader("Origin"));
servletResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE,PATCH");
servletResponse.setHeader("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,SessionToken,Cookie");
servletResponse.setHeader("Access-Control-Max-Age", "3600");
servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
servletResponse.setHeader("Access-Control-Expose-Headers", "*");
//判断是否为可选择性批量操作的请求,设置状态码返回
if ("OPTIONS".equals(servletRequest.getMethod())) {
servletResponse.setStatus(HttpStatus.ACCEPTED.value());
return;
}
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
加上之后 让我们再来Look Look
可以看到访问成功啦。
上一篇: centos7.4下cmake源码安装
下一篇: 【剑指offer】40.最小的K个数