日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
SpringBoot中CORS問(wèn)題及解決辦法,源碼解析

CORS(跨源資源共享)是一種Web標(biāo)準(zhǔn),允許來(lái)自不同源的Web頁(yè)面共享資源。在Spring Boot應(yīng)用程序中,CORS問(wèn)題可能會(huì)出現(xiàn),因?yàn)闉g覽器會(huì)阻止來(lái)自不同源的請(qǐng)求。默認(rèn)情況下,Spring Boot允許來(lái)自同一源的請(qǐng)求,但會(huì)阻止來(lái)自不同源的請(qǐng)求。

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)桑珠孜,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108

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

使用全局CORS配置

您可以在Spring Boot應(yīng)用程序的主類上添加@CrossOrigin注解,以允許來(lái)自所有源的請(qǐng)求。例如:

@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("*");
            }
        };
    }
}

在上面的示例中,我們創(chuàng)建了一個(gè)WebMvcConfigurer bean,并覆蓋了addCorsMappings方法。我們使用CorsRegistry對(duì)象來(lái)定義CORS規(guī)則。在這個(gè)例子中,我們?cè)试S來(lái)自所有源的請(qǐng)求,并允許所有方法和頭部。

使用局部CORS配置

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

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

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

在上面的示例中,我們只在getData方法上啟用了CORS。我們?cè)试S來(lái)自所有源的請(qǐng)求,并允許所有方法和頭部。

使用自定義CORS配置

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

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            CorsConfiguration config = new CorsConfiguration();
            config.setAllowedOrigins(Arrays.asList("http://cdxwcx.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);
        }
    };
}

在上面的示例中,我們創(chuàng)建了一個(gè)自定義的CorsConfiguration對(duì)象,并設(shè)置了允許的源、方法、頭部和憑證。然后,我們將該配置添加到CorsRegistry對(duì)象中,以應(yīng)用于所有的請(qǐng)求路徑。
除了上述方法,還有一些其他的解決方案可以用來(lái)解決Spring Boot中的CORS問(wèn)題。例如:

使用Spring Security的CORS支持

如果您正在使用Spring Security,您可以使用其提供的CORS支持來(lái)解決CORS問(wèn)題。以下是一個(gè)示例配置:

@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://cdxwcx.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;
    }
}

在上面的示例中,我們創(chuàng)建了一個(gè)CorsConfigurationSource bean,并設(shè)置了允許的源、方法、頭部和憑證。然后,我們?cè)贖ttpSecurity對(duì)象上調(diào)用cors()方法來(lái)啟用CORS支持,并將CorsConfigurationSource對(duì)象傳遞給該方法。

使用過(guò)濾器解決CORS問(wèn)題

您還可以創(chuàng)建一個(gè)自定義的過(guò)濾器來(lái)解決CORS問(wèn)題。以下是一個(gè)示例配置:

@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://cdxwcx.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);
    }
}

在上面的示例中,我們創(chuàng)建了一個(gè)自定義的CorsFilter類,并覆蓋了doFilterInternal方法。在這個(gè)方法中,我們創(chuàng)建了一個(gè)CorsConfiguration對(duì)象,并設(shè)置了允許的源、方法、頭部和憑證。然后,我們創(chuàng)建了一個(gè)UrlBasedCorsConfigurationSource對(duì)象,并將CorsConfiguration對(duì)象注冊(cè)到該對(duì)象中。最后,我們創(chuàng)建了一個(gè)CorsFilter對(duì)象,并將其應(yīng)用到請(qǐng)求/響應(yīng)鏈中。


網(wǎng)站標(biāo)題:SpringBoot中CORS問(wèn)題及解決辦法,源碼解析
文章地址:http://m.5511xx.com/article/djpphis.html