springcloud跨域(分环境)解决方案

博客介绍了利用Spring Cloud解决跨域问题的方法。通过在配置文件或配置中心添加name属性,测试服务器配置为允许跨域,生产服务器配置为拒绝跨域,利用havingValue属性使@Configuration生效,解决了测试服允许全局跨域、正式服不允许的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springcloud跨域(分环境)解决方案


配置类 CorsConfig.java.

package com.test.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Scen
 * @date 2019-05-27 17:25
 */
@Configuration
@ConditionalOnProperty(name = "open.cors", havingValue = "true")
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 是否允许证书 不再默认开启
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(7200);
    }
}

其中

@ConditionalOnProperty(name = "open.cors", havingValue = "true")

name属性是从配置文件application.properties或者配置中心添加的,测试服务器配置true允许跨域,
生产服务器配置false拒绝跨域,havingValue属性定义了只有当name属性的值为havingValue的值时才使得@Configuration生效,这就解决了测试服允许全局跨域正式服不允许全局跨域的问题

### Spring Cloud 配置方法 在 Spring Cloud 中,问题(CORS, Cross-Origin Resource Sharing)可以通过多种方式进行配置。一种常见的方式是在网关层面上通过 `CorsGlobalFilter` 配置全局的支持[^1]。 #### 使用 CorsGlobalFilter 进行全局配置 下面展示了一个利用 `CorsGlobalFilter` 实现访问控制的例子: ```java import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.filter.CorsGlobalProperties; @Bean public CorsGlobalProperties corsGlobalProperties() { CorsGlobalProperties properties = new CorsGlobalProperties(); CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedHeader("*"); configuration.addAllowedMethod("*"); configuration.setAllowCredentials(true); configuration.addAllowedOriginPattern("*"); properties.setAddToSimpleRequests(false); // 可选,默认false properties.setCorsConfigurations(Collections.singletonMap("/**", configuration)); return properties; } ``` 此段代码创建了允许任何源、头部和HTTP 方法发起请求并携带凭证的 CORS 设置,并将其应用到所有的路径上。 另一种方式则是直接定义一个 `WebMvcConfigurer` Bean 来添加映射规则,这种方式适用于更细粒度地管理哪些 URL 模式应该启用 CORS 政策[^4]: ```java @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .maxAge(TimeUnit.DAYS.toSeconds(1)); } }; } ``` 上述两种方案都可以有效地解决 Spring Cloud 应用中的资源共享问题。具体采用哪种取决于项目需求和个人偏好[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值