Spring Boot如何允许所有域名都能跨域访问并能使用credentials

使用@CrossOrigin(origins="*")时会有响应头Access-Control-Allow-Origin: *。这时前端设置withCredential 会报错Failed to load http://localhost:8080/test: 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'. Origin 'http://localhost:3000' is therefore not allowed access.
能否对所有的请求网站都设置对应的响应头,如localhost:3000访问就设置Access-Control-Allow-Origin: localhost:3000

阅读 5.3k
1 个回答

被告知@CrossOrigin标注无法实现,可以通过过滤器Filter实现。
看了下Filter确实可以实现,mdn有简单的示例。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题