鍍金池/ 問答/Java  HTML/ Spring Boot如何允許所有域名都能跨域訪問并能使用credentials

Spring Boot如何允許所有域名都能跨域訪問并能使用credentials

使用@CrossOrigin(origins="*")時(shí)會(huì)有響應(yīng)頭Access-Control-Allow-Origin: *。這時(shí)前端設(shè)置withCredential 會(huì)報(bào)錯(cuò)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.
能否對(duì)所有的請(qǐng)求網(wǎng)站都設(shè)置對(duì)應(yīng)的響應(yīng)頭,如localhost:3000訪問就設(shè)置Access-Control-Allow-Origin: localhost:3000

回答
編輯回答
吢丕

被告知@CrossOrigin標(biāo)注無法實(shí)現(xiàn),可以通過過濾器Filter實(shí)現(xiàn)。
看了下Filter確實(shí)可以實(shí)現(xiàn),mdn有簡單的示例。

2017年7月14日 06:33