allowedOrigins cannot contain the special value "*"

Spring Boot的版本高于 2.4以后 ,原来的配置已经不适合目前的版本
将代码中的allowedOrigins改为allowedOriginPatterns


@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 跨域支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

}

修改后

@Configuration
public class WebConfig implements WebMvcConfigurer {


    /**
     * 跨域支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

}

posted @ 2023-09-01 09:03  VipSoft  阅读(115)  评论(0编辑  收藏  举报