码迷,mamicode.com
首页 > 其他好文 > 详细

跨域过滤器

时间:2019-09-25 12:18:38      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:class   requested   etc   options   return   tco   wrap   except   token   

@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() {
}
}

技术图片

跨域过滤器

标签:class   requested   etc   options   return   tco   wrap   except   token   

原文地址:https://www.cnblogs.com/-mzh/p/11583598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!