解决springcloud微服务架构系统中和单体springboot架构配置全局CORS。

解决微服务系统中配置全局CORS。

只需要在api gateway的application.properties里面配置:

spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods=GET,POST,PUT,DELETE,OPTIONS
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedHeaders=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=false

allowedOrigins后面可以是*,表示允许所有的origin,也可以配置具体的doman例如:http://your-frontend.com。注意:只需要在api gateway里面配置,其他后端service米面不需要在配置cros。

在单体架构架构中配置全局cors

@Configuration
public class GlobalCorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost", "http://your-frontend.com", "null") // Specify your allowed origins here
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true); // Enable credentials if needed
    }
}

allowedOrigins也可以设置"*",表示允许所有的origin。

在controller或者具体的API上配置CORS

主要加在controller和具体的API上面加上注解like : @CrossOrigin(origins = “http://localhost:5000”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值