Springboot通過cors解決跨域問題(解決spring security oath2的/oauth/token跨域問題)


@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 允許cookies跨域
config.addAllowedOrigin("*");// 允許向該服務器提交請求的URI,*表示全部允許。。這里盡量限制來源域,比如http://xxxx:8080 ,以降低安全風險。。
config.addAllowedHeader("*");// 允許訪問的頭信息,*表示全部
config.setMaxAge(18000L);// 預檢請求的緩存時間(秒),即在這個時間段里,對於相同的跨域請求不會再預檢了
config.addAllowedMethod("*");// 允許提交請求的方法,*表示全部允許,也可以單獨設置GET、PUT等
/* config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");// 允許Get的請求方法
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");*/
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}


免責聲明!

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



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