互联网技术-alibaba-gateway网关中过滤器开发以及默认过滤器filter

简介

过滤器开发

1.gateway 通过 GlobalFilter 编写全局过滤器, 通过Ordered 编写过滤器的优先级(越大,优先级越低)

package com.czxy.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * @author 桐叔
 * @email liangtong@itcast.cn
 */
@Component
public class DemoFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1 获得请求路径
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();
        System.out.println(path);

        // 2 处理异常
        if("/service-consumer/feign/echo/abc".equals(path)) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            response.getHeaders().add("Content-Type","application/json;charset=UTF-8");
            DataBuffer wrap = response.bufferFactory().wrap("没有权限".getBytes(StandardCharsets.UTF_8));
            return exchange.getResponse().writeWith(Flux.just(wrap));
        }

        // 3 放行
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            System.out.println("最后处理");
        }));
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

 默认过滤器

默认路由的配置,将应用到所有路由上:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#default-filters

默认路由配置:设置请求头

spring:
  cloud:
    gateway:
      default-filters:
        - AddRequestHeader=czxy,xxxx

修改TestFeignController打印请求头

package com.czxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.czxy.feign.EchoFeign;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 桐叔
 * @email liangtong@itcast.cn
 */
@RestController
@RequestMapping("/feign")
public class TestFeignController {
    @Resource
    private EchoFeign echoFeign;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

//    @GetMapping("/echo/{str}")
//    @PostMapping("/echo/{str}")
    @RequestMapping(value = "/echo/{str}", method = {RequestMethod.GET, RequestMethod.POST})
    @SentinelResource("/feign/echo")
    public String echo(@PathVariable String str) {
        // 打印cookie
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        // 打印请求头
        System.out.println("head:" + request.getHeader("czxy"));
        // 设置cookie
        Cookie cookie = new Cookie("czxy","jack");
        response.addCookie(cookie);
        return echoFeign.echo(str);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值