Gateway定义全局过滤器以及gateway限流以及如何配置跨域

方式一:直接编写一个过滤工厂,重写方法编写过滤器

 方式二:编写过滤器工厂,再写一个过滤器类实现gatewayfilter重写方法

package sca.pro.gateway.common.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

/**
 * @author shq
 * @description 自定义token认证过滤器工厂
 * @createDate 2022-5-25
 * @updateUser
 * @updateDate
 * @updateRemark
 */
@Component
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Autowired
    private AuthGatewayFilter authGatewayFilter;

    @Override
    public GatewayFilter apply(Object config) {
        return authGatewayFilter;
    }
}
package sca.pro.gateway.common.auth;


import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import sca.pro.common.contants.Contants;


/**
 * @author shq
 * @description 身份认证过滤器
 * @createDate 2022-5-25
 * @updateUser
 * @updateDate
 * @updateRemark
 */
@Component
public class AuthGatewayFilter implements GatewayFilter, Ordered {
    /**
     * 排序规则
     *
     * @return
     */
    @Override
    public int getOrder() {
        return -100;
    }

    /**
     * jwt全局过滤器
     *
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        MultiValueMap<String, String> headers = exchange.getRequest().getHeaders();
        if (!CollectionUtils.isEmpty(headers) && headers.containsKey(Contants.HEADER_LOGIN_INFO_BASE64_KEY)) {
            ServerHttpRequest host = exchange.getRequest().mutate().header(Contants.HEADER_LOGIN_INFO_BASE64_KEY, null).build();
            exchange = exchange.mutate().request(host).build();
        }
        return chain.filter(exchange);
    }
}

注意这里的配置文件,过滤器的填写格式,就是定义的过滤器工厂类的前缀,比如咱们是AuthGatewayFilterFactory,那么在配置文件就要写成Auth,例如:

spring:
  cloud:
    gateway:
      routes:
        - id: sca-jwt-local
          uri: lb://sca-jwt-local
          predicates:
            - Path=/jwt/**
          filters:
//当配置了 StripPrefix=1 过滤器后,如果请求的路径中存在前缀路径,该过滤器将会去除指定数量的前缀路径。具体而言,StripPrefix=1 表示去除请求路径的第一个路径片段(即前缀路径)
//当收到形如 /jwt/users 的请求时,StripPrefix=1 将会去除 /api 前缀,将最终的请求路径转发到http://sca-jwt-local/users
            - StripPrefix=1
            - Auth
   gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow- 
   Credentials,
      globalcors:
        cors-configurations:
          '[/**]':
            allowed-origins: "*"
            allowed-methods: "*"
            allowed-headers: "x-requested-with,Content-Language,Authorization,token,Access-Control-Allow-Origin,request-id,Accept-Language,content-type,content-disposition"
            allow-credentials: true

到此全局过滤器已经定义完成了

下边来看一下限流的方式

Spring Cloud Gateway限流详解_慕课手记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Gateway服务中进行配置有多种方法。一种方法是通过在网关的配置类中进行配置。具体的配置可以在yaml文件中查看,例如在org.springframework.cloud.gateway.config.GlobalCorsProperties类中配置。可以设置allowedOrigins来允许哪些网站的请求,allowedMethods来允许的ajax请求方式,allowedHeaders来允许在请求中携带的头信息,allowCredentials来确定是否允许携带cookie,maxAge来设置检测的有效期。\[2\] 另一种方法是在Gateway服务的application.yml文件中进行配置。可以使用spring.cloud.gateway.globalcors来配置全局处理,设置allowedOrigins来允许哪些网站的请求,allowedMethods来允许的ajax请求方式,allowedHeaders来允许在请求中携带的头信息,allowCredentials来确定是否允许携带cookie,maxAge来设置检测的有效期。\[3\] 总之,通过配置类或在application.yml文件中进行配置,可以实现Gateway服务的配置。 #### 引用[.reference_title] - *1* *2* [Spring Cloud Gateway 解决问题](https://blog.csdn.net/weixin_45481406/article/details/127573858)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Gateway网关-网关的cors配置](https://blog.csdn.net/Leon_Jinhai_Sun/article/details/121186047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EntyIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值