springboot项目全局开启允许跨域

渡星河
2023-12-20 / 0 评论 / 10 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年12月20日,已超过273天没有更新,若内容或图片失效,请留言反馈。

1.随便找个位置,必须和启动类同级,否则扫描不到

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

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
2

评论 (0)

取消