关于javascript:当请求的凭据模式为包含时响应中的标头不能是通配符‘‘

5次阅读

共计 1315 个字符,预计需要花费 4 分钟才能阅读完成。

欢送拜访 moshuying.top 查看更多信息

具体错误信息 Access to XMLHttpRequest at 'http://localhost:7894/Login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the'Access-Control-Allow-Origin'header in the response must not be the wildcard'*'when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

前端 vue-antd-admin
后端 java servlet

<!– more –>

前后端拆散的我的项目中必定会碰到跨域的问题,跨域的起因还是为了平安。最近在做一个我的项目的时候发现,即便我曾经设置了跨域信息,前端还是报这个跨域谬误。

起初找了有教训的后盾来帮忙也是弄了很久都没有解决问题,起初我缓缓看申请头和认真寻找前后端的代码终于发现了端倪。

我我的项目中失败的申请头

参考自 MDN 中失败的一个申请头

能够发现多了 AuthorizationAccept两个申请头,这两个申请头来自前端我的项目中的 src/utils/request.js 对申请经行了简略的平安设置,使得申请中携带了平安信息,查看源代码发现代码中设置了

axios.defaults.withCredentials= true

示意客户端想要携带验证信息,这部分性能须要后盾反对,而后盾 supportsCredentials 个别被设置为 false 即不反对客户端携带验证信息

对后盾代码进行批改

protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {res.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
    res.addHeader("Access-Control-Allow-Methods", "*");
    res.addHeader("Access-Control-Allow-Headers", "Accept,Authorization,DNT,Content-Type,Referer,User-Agent");
    res.addHeader("Access-Control-Allow-Credentials","true"); // 容许携带验证信息
    chain.doFilter(req, res);
}

问题解决

参考链接

mozilla 的 HTTP 访问控制
介绍了跨域中其余的头信息

正文完
 0