日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Spring Boot中CORS問題及解決辦法,源碼解析

來源: 責編: 時間:2023-11-03 09:17:19 320觀看
導讀CORS(跨源資源共享)是一種Web標準,允許來自不同源的Web頁面共享資源。在Spring Boot應用程序中,CORS問題可能會出現,因為瀏覽器會阻止來自不同源的請求。默認情況下,Spring Boot允許來自同一源的請求,但會阻止來自不同源的請

kOj28資訊網——每日最新資訊28at.com

CORS(跨源資源共享)是一種Web標準,允許來自不同源的Web頁面共享資源。在Spring Boot應用程序中,CORS問題可能會出現,因為瀏覽器會阻止來自不同源的請求。默認情況下,Spring Boot允許來自同一源的請求,但會阻止來自不同源的請求。kOj28資訊網——每日最新資訊28at.com

要解決CORS問題,您可以使用Spring Boot提供的CORS支持。以下是一些可能的解決方案:kOj28資訊網——每日最新資訊28at.com

使用全局CORS配置

您可以在Spring Boot應用程序的主類上添加@CrossOrigin注解,以允許來自所有源的請求。例如:kOj28資訊網——每日最新資訊28at.com

@SpringBootApplicationpublic class MyApplication {    public static void main(String[] args) {        SpringApplication.run(MyApplication.class, args);    }    @Bean    public WebMvcConfigurer corsConfigurer() {        return new WebMvcConfigurer() {            @Override            public void addCorsMappings(CorsRegistry registry) {                registry.addMapping("/**")                        .allowedOrigins("*")                        .allowedMethods("*")                        .allowedHeaders("*");            }        };    }}

在上面的示例中,我們創建了一個WebMvcConfigurer bean,并覆蓋了addCorsMappings方法。我們使用CorsRegistry對象來定義CORS規則。在這個例子中,我們允許來自所有源的請求,并允許所有方法和頭部。kOj28資訊網——每日最新資訊28at.com

使用局部CORS配置

如果您只想為特定的控制器或請求方法啟用CORS,您可以在控制器類或請求方法上添加@CrossOrigin注解。例如:kOj28資訊網——每日最新資訊28at.com

@RestController@RequestMapping("/api")public class MyController {    @CrossOrigin(origins = "*", methods = "*", headers = "*")    @GetMapping("/data")    public ResponseEntity<String> getData() {        // ...    }}

在上面的示例中,我們只在getData方法上啟用了CORS。我們允許來自所有源的請求,并允許所有方法和頭部。kOj28資訊網——每日最新資訊28at.com

使用自定義CORS配置

如果您需要更細粒度的CORS配置,您可以創建自定義的CorsConfiguration對象,并將其添加到CorsRegistry對象中。例如:kOj28資訊網——每日最新資訊28at.com

@Beanpublic WebMvcConfigurer corsConfigurer() {    return new WebMvcConfigurer() {        @Override        public void addCorsMappings(CorsRegistry registry) {            CorsConfiguration config = new CorsConfiguration();            config.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org"));            config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));            config.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization"));            config.setAllowCredentials(true);            registry.addMapping("/**").withConfig(config);        }    };}

在上面的示例中,我們創建了一個自定義的CorsConfiguration對象,并設置了允許的源、方法、頭部和憑證。然后,我們將該配置添加到CorsRegistry對象中,以應用于所有的請求路徑。
除了上述方法,還有一些其他的解決方案可以用來解決Spring Boot中的CORS問題。例如:
kOj28資訊網——每日最新資訊28at.com

使用Spring Security的CORS支持

如果您正在使用Spring Security,您可以使用其提供的CORS支持來解決CORS問題。以下是一個示例配置:kOj28資訊網——每日最新資訊28at.com

@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.cors().and(). ...    }    @Bean    public CorsConfigurationSource corsConfigurationSource() {        CorsConfiguration configuration = new CorsConfiguration();        configuration.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org"));        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));        configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization"));        configuration.setAllowCredentials(true);        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        source.registerCorsConfiguration("/**", configuration);        return source;    }}

在上面的示例中,我們創建了一個CorsConfigurationSource bean,并設置了允許的源、方法、頭部和憑證。然后,我們在HttpSecurity對象上調用cors()方法來啟用CORS支持,并將CorsConfigurationSource對象傳遞給該方法。kOj28資訊網——每日最新資訊28at.com

使用過濾器解決CORS問題

您還可以創建一個自定義的過濾器來解決CORS問題。以下是一個示例配置:kOj28資訊網——每日最新資訊28at.com

@Componentpublic class CorsFilter extends OncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {        CorsConfiguration config = new CorsConfiguration();        config.setAllowedOrigins(Arrays.asList("http://example.com", "https://example.org"));        config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));        config.setAllowedHeaders(Arrays.asList("X-Requested-With", "Content-Type", "Authorization"));        config.setAllowCredentials(true);        CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        source.registerCorsConfiguration("/**", config);        CorsFilter corsFilter = new CorsFilter(source);        corsFilter.doFilter(request, response, filterChain);    }}

在上面的示例中,我們創建了一個自定義的CorsFilter類,并覆蓋了doFilterInternal方法。在這個方法中,我們創建了一個CorsConfiguration對象,并設置了允許的源、方法、頭部和憑證。然后,我們創建了一個UrlBasedCorsConfigurationSource對象,并將CorsConfiguration對象注冊到該對象中。最后,我們創建了一個CorsFilter對象,并將其應用到請求/響應鏈中。kOj28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-16753-0.htmlSpring Boot中CORS問題及解決辦法,源碼解析

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 用積分神經網絡在一分鐘內轉換DNN

下一篇: 程序員福音——CodeGeeX智能編程助手

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 祁连县| 伊金霍洛旗| 锡林郭勒盟| 色达县| 博野县| 壤塘县| 娄底市| 大余县| 罗田县| 安义县| 密山市| 彩票| 揭西县| 明光市| 延安市| 曲阜市| 蓬莱市| 女性| 柘城县| 肃北| 陆川县| 黄龙县| 三原县| 昌邑市| 巩义市| 达州市| 丘北县| 疏勒县| 苗栗县| 门源| 玛纳斯县| 昌江| 寿阳县| 陈巴尔虎旗| 彭水| 永丰县| 凌云县| 普洱| 孝感市| 赣州市| 正蓝旗|