springcloud gateway引入CORS

springcloud gateway引入cors(全称Cross-Origin Resource Sharing,跨站资源共享),是用于限制资源访问的一种方式,比如我们限制某些接口或者图片资源不被除允许外的站点访问,springcloud gateway做了访问系统资源的第一道门槛,CORS功能一般都会放在springcloud gateway应用中,而不是放在单独的引用中,springcloud gateway引入cors配置有两种方式:

1、配置,比如

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

2、代码设置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;

/**
 * @author: john
 * @description: com.jtl3d.config
 * @createdTime: 2019/12/20 15:24
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("www.jtljia.com");
        config.addAllowedHeader("*");
        config.setMaxAge(18000L);
        config.addAllowedMethod("POST");
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

 

注:笔者在使用springcloud gateway时使用的是webflux模式,不是系统的web模式,如果使用sprin-webmvc则使用CorsFilter

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值