SpringBoot 允许跨域

解决办法

@SpringBootApplication
@MapperScan("com.humorchen.pastry_examination.mapper")
public class PastryExaminationApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(PastryExaminationApplication.class, args);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowedMethods("*");
    }
}

新版本springboot跨域解决办法,把这个配置bean注入就可以了


@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedHeaders("*")
                        .allowedMethods("*")
                        .allowedOrigins("*")
                        .allowCredentials(true);
            }
        };
    }
posted @ 2021-06-12 12:24  HumorChen99  阅读(1)  评论(0编辑  收藏  举报  来源