成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 架構
在Spring Boot應用程序中,CORS問題可能會出現,因為瀏覽器會阻止來自不同源的請求。默認情況下,Spring Boot允許來自同一源的請求,但會阻止來自不同源的請求。

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

要解決CORS問題,您可以使用Spring Boot提供的CORS支持。以下是一些可能的解決方案:

使用全局CORS配置

您可以在Spring Boot應用程序的主類上添加@CrossOrigin注解,以允許來自所有源的請求。例如:

@SpringBootApplication
public 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規則。在這個例子中,我們允許來自所有源的請求,并允許所有方法和頭部。

使用局部CORS配置

如果您只想為特定的控制器或請求方法啟用CORS,您可以在控制器類或請求方法上添加@CrossOrigin注解。例如:

@RestController
@RequestMapping("/api")
public class MyController {

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

在上面的示例中,我們只在getData方法上啟用了CORS。我們允許來自所有源的請求,并允許所有方法和頭部。

使用自定義CORS配置

如果您需要更細粒度的CORS配置,您可以創建自定義的CorsConfiguration對象,并將其添加到CorsRegistry對象中。例如:

@Bean
public 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問題。例如:

使用Spring Security的CORS支持

如果您正在使用Spring Security,您可以使用其提供的CORS支持來解決CORS問題。以下是一個示例配置:

@Configuration
@EnableWebSecurity
public 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對象傳遞給該方法。

使用過濾器解決CORS問題

您還可以創建一個自定義的過濾器來解決CORS問題。以下是一個示例配置:

@Component
public 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對象,并將其應用到請求/響應鏈中。

責任編輯:姜華 來源: 今日頭條
相關推薦

2011-05-12 13:34:57

SQL Server

2010-09-07 09:50:35

DIVCSS

2010-03-04 16:49:44

2009-02-18 09:30:10

AJAX跨域XML

2015-04-09 17:44:10

APP性能解決辦法APP

2011-03-04 13:49:38

FileZilla

2010-01-27 12:06:00

UPS常見故障

2012-05-30 16:19:11

2011-05-19 14:16:29

網頁設計

2012-03-20 10:21:01

App Store刷排名

2017-06-21 08:30:20

MySQL原因解決辦法

2010-06-10 15:21:30

openSUSE聲音

2012-07-31 16:06:28

Linux內核編譯

2012-12-12 15:19:32

云安全

2010-05-17 17:45:54

MySQL亂碼問題

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2010-03-09 15:02:04

2015-03-09 15:41:08

MongoDB查詢超時異常Socket Time

2011-03-04 13:07:47

Filezilla

2011-10-28 10:56:24

jQTouchjQueryiPhone
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区精品在线观看 | 亚洲国产区| 亚洲综合五月天婷婷 | 久久久精品在线 | 国产成人免费视频 | 免费成人高清在线视频 | 丁香久久 | 日韩欧美国产一区二区三区 | 一级片在线视频 | 国产中文字幕在线 | h网站在线观看 | 伊人久久精品 | 91麻豆精品国产91久久久更新资源速度超快 | 成人不卡在线 | 国产精品久久亚洲7777 | 中文字幕一区在线观看视频 | 欧美激情久久久 | 在线天堂免费中文字幕视频 | 欧美精品网站 | 天天爽一爽 | 伊人网99 | 高清国产一区二区 | 欧美一级黄色网 | 日本一二三区电影 | 亚洲成人一二区 | 久草视频网站 | 日韩av一区二区在线观看 | 亚洲国产成人精品久久久国产成人一区 | 国产最新精品视频 | 国产精品1区 | 久综合 | 超碰免费观看 | 亚洲成人毛片 | 操操网站| 免费av一区二区三区 | www国产成人免费观看视频 | 国产一区2区 | 国产一区91在线 | 在线看黄免费 | 狠狠入ady亚洲精品经典电影 | 久久久久久亚洲精品 |