一、开发环境
vue springmvc cas
二、问题描述
请求头为 'Content-Type': 'application/json;charset=utf-8' 的 $http.post 无法正常查询数据总是返回 302 ,参考网上的教程添加任何过滤器都不好使。 各种猜测折腾好几天。
Access to XMLHttpRequest at 'http://127.0.0.1:10010/wyxt_ubqts_oncloud/docDetail/checkDocList' from origin 'http://127.0.0.1:2019' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
三、问题解决
通过自定义filter 过滤 options 方式的请求发现 在cas之后无法拦截,放在cas之前设置的 header 又不好使,只好修改 cas的filter ,反编译 AuthenticationFilter ,复制内容,新建 CasAuthenticationFilter 类 。
在doFilter 方法 最开始 添加
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin") );
response.setHeader("Access-Control-Allow-Methods", "OPTIONS,HEAD,DELETE,GET,PUT,POST");
response.setHeader("Access-Control-Max-Age", "1728000");
response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Authorization,WG-App-Version,WG-Device-Id,WG-Network-Type,WG-Vendor,WG-OS-Type,WG-OS-Version,WG-Device-Model,WG-CPU,WG-Sid,WG-App-Id,WG-Token");
web.xml 的配置 AuthenticationFilter 改为 CasAuthenticationFilter。
重启,数据正常请求!