在使用CORS处理跨域问题时报错
The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8088, *', but only one is allowed. Origin 'http://localhost:8088' is therefore not allowed access.
错误显示有两个值,说明后台处理了两次,一查还真是,在过滤器中处理了一次在拦截器中又处理了一次,只要去掉其中一个就可以。
跨域处理方法如下:在web.xml中设置过滤器,并配置为拦截所有
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request=(HttpServletRequest)servletRequest;
response.setContentType("textml;charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "0");
response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("XDomainRequestAllowed","1");
log.info(" request.getHeader====="+ request.getHeader("Origin"));
filterChain.doFilter(servletRequest,servletResponse);
}