跨域过滤器
Posted -mzh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨域过滤器相关的知识,希望对你有一定的参考价值。
@Component
public class CorsFilter implements Filter
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, GET");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Allow-Headers", "token,userId,Content-Type,x-is-mini-program-environment");
response.setHeader("Access-Control-Allow-Credentials", "true");
// HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper((HttpServletRequest) request)
// /**
// */
// @Override
// public String getContentType()
// // 先从原本的Request中获取头,如果为空且名字为token,则从参数中查找并返回
// String contentType = super.getContentType();
//
//// if("Content-Type".equals(name))
//// String contentType = request.getHeader("Content-Type");
// return contentType.replace("multipart/form-data,","");
////
//// return superHeader;
//
// ;
// request.getContentType(); //setAttribute("Content-Type", contentType.replace("multipart/form-data,",""));
chain.doFilter(request, res);
@Override
public void init(FilterConfig filterConfig)
@Override
public void destroy()
以上是关于跨域过滤器的主要内容,如果未能解决你的问题,请参考以下文章
spring 设置跨域 CORS (Cross Origin Resources Share) 跨域