文章正文

JAVA-跨域问题及解决方案一

【文章】2020-04-23

简介JAVA-跨域问题及解决方案一

问题:

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.

思路:
当前端对请求行安全设置如下:

axios.defaults.withCredentials= true

表示客户端想要携带验证信息,这部分功能需要后台支持,而后台支持的Credentials默认设置为false即不支持客户端携带验证信息

后台改造:

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "PATCH,HEAD,POST, GET, OPTIONS, DELETE,PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Headers", "token,accessToken,finger,XMLHttpRequest,x-requested-with,X_Requested_With, AcceptAccept,Authorization,DNT,Content-Type,Referer,User-Agent");

备注:token,accessToken,finger为自定义验证信息,若定义为其他命名,需要在header中添加。

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速查找!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:30,若离线请留言!

    PDF电子书