搭建NFS服務
阿新 • • 發佈:2022-04-15
在Springboot專案里加上這個配置檔案CorsConfig.java
,重啟之後即可實現跨域訪問,前端無需再配置跨域。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { // 當前跨域請求最大有效時長。這裡預設1天 private static final long MAX_AGE = 24 * 60 * 60; @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration= new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1 設定訪問源地址 corsConfiguration.addAllowedHeader("*"); // 2 設定訪問源請求頭 corsConfiguration.addAllowedMethod("*"); // 3 設定訪問源請求方法 corsConfiguration.setMaxAge(MAX_AGE); source.registerCorsConfiguration("/**", corsConfiguration); //4 對介面配置跨域設定 return new CorsFilter(source); } }