后端跨域配置

添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

CorsFilter 才可以配置引用
 

@Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter(){
CorsConfiguration config=new CorsConfiguration();
//允许就收的域
config.addAllowedOrigin("http://localhost:8080");
//是否发送Cookie信息
config.setAllowCredentials(true);
//允许方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
//允许的头信息
config.addAllowedHeader("*");
//添加映射路线,拦截一切请求
UrlBasedCorsConfigurationSource configSource=new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**",config);
//添加新的CorsFilter
return new CorsFilter(configSource);
}
}
posted @ 2024-02-24 01:57  hongdouni  阅读(32)  评论(0)    收藏  举报