SpringBoot支持跨域请求

1.springboot要版本保持在2.5以上

2.添加跨域配置

CorsConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowCredentials(true)
                        .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS","PATCH")
                        .allowedHeaders("*")
                        .maxAge(3600);
                WebMvcConfigurer.super.addCorsMappings(registry);
            }
        };
    }
}

3.启动验证测试

百度首页测试
SpringBoot支持跨域请求

上一篇:【Android】记录一次安卓10的图片读取


下一篇:chrome浏览器91版本SameSite by default cookies被移除后的解决方案,Chrome中跨域POST请求无法携带Cookie的解决方案