//1.需要配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
//2.yml文件配置
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: service-acl #nacos中的服务名
uri: lb://service-acl #转发的路径
predicates:
- Path=/*/acl/** #断言,若路径包含acl则转发给service-acl处理
//3.将原来控制器上的@CrossOrigin注解全部去掉
在使用了spring-cloud-starter-gateway后,为什么还会发生cors问题
最新推荐文章于 2024-07-30 19:07:40 发布