SpringBoot跨域問題:When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin"


When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

意思是:當allowCredentials為true時,allowingOrigins不能包含特殊值“ *”,因為無法在“ Access-Control-Allow-Origin”響應標頭上設置。要允許憑據具有一組來源,請明確列出它們或考慮改用“ allowedOriginPatterns”。

解決:將allowingOrigins換成allowedOriginPatterns即可。

修改前:

public CorsFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    //允許所有域名進行跨域調用
    config.addAllowedOrigin("*");
    //允許跨越發送cookie
    config.setAllowCredentials(true);
    //放行全部原始頭信息
    config.addAllowedHeader("*");
    //允許所有請求方法跨域調用
    config.addAllowedMethod("*");
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

修改后:

public CorsFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    //允許所有域名進行跨域調用
    config.addAllowedOriginPattern("*");//替換這個
    //允許跨越發送cookie
    config.setAllowCredentials(true);
    //放行全部原始頭信息
    config.addAllowedHeader("*");
    //允許所有請求方法跨域調用
    config.addAllowedMethod("*");
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM