@SpringBootApplication
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
/**
* 配置跨域访问的过滤器
* @return
*/
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("PUT", "DELETE","GET","POST")
.allowedHeaders("*")
.exposedHeaders("access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-max-age",
"X-Frame-Options")
.allowCredentials(false).maxAge(3600);
}
};
}
}
springboot,解决跨域问题
最新推荐文章于 2024-10-07 18:47:58 发布
本文详细介绍了一种在SpringBoot项目中实现跨域访问的方法,通过配置WebMvcConfigurerAdapter来允许所有来源的请求,包括GET、POST、PUT和DELETE等HTTP方法,并暴露了多个头部信息,确保了前后端分离项目的顺利运行。
1025

被折叠的 条评论
为什么被折叠?



