跨域中"Access-Control-Allow-Origin"设置了依然报错原因

在使用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);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值