嘿,我正在構建一個回應式網站,我嘗試從我在 spring 中構建的后端服務器獲取資料,但它不起作用。我將 CorsConfigurationSource 添加到我的安全配置中,但它仍然不起作用。我的 cors 安全配置:
@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfiguration corsConfiguration=new CorsConfiguration();
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowedMethods(Arrays.asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
我最終得到的錯誤:https://i.stack.imgur.com/olGCD.png\ 我的獲取請求(我正在發布用戶憑據):
fetch("http://localhost:8080/login", {
method: "POST",
mode:'cors',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams(tempLoginCredentials),
})
uj5u.com熱心網友回復:
Google Chrome 中的 CORS 在localhost. 在此處閱讀更多相關資訊。
最重要的是,PORT 在 CORS 中確實很重要。請注意,您在 Spring 配置中啟用了埠 3000,但正在獲取 8080。
uj5u.com熱心網友回復:
我認為您需要更多標題和 OPTIONS 方法。
config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Authorization"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/480281.html
標籤:javascript 弹簧靴 http 弹簧安全 科尔斯
