Spring Cloud Gateway 3 和 Spring WebFlux 常用操作

Spring Cloud Gateway 3 和 Spring WebFlux 中拦截器直接响应结果

private Mono<Void> returnNotLogin(ServerWebExchange exchange) {
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        DataBuffer wrap = response.bufferFactory().wrap(
            JSON.toJSONString(ServerResponseEntity.fail(ResponseEnum.NOT_AUTH), JSONWriter.Feature.WriteMapNullValue)
                .getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Flux.just(wrap));
    }

Spring Cloud Gateway 3 和 Spring WebFlux 中不同过滤器间传递参数、修改gateway转发请求头

@Slf4j
@Component
public class InternalServiceAuthFilter implements GatewayFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 不同过滤器间传递参数
        exchange.getAttributes().put("","");
        Object attribute = exchange.getAttribute(Constant.W3_LOGIN_IS_SUCCESS_KEY);
        ServerHttpRequest request = exchange.getRequest();
        
        // 修改gateway转发请求头
        ServerHttpRequest.Builder mutate = request.mutate();
        mutate.header(Constant.OPENX_USER_INFO_HEADER_NAME, JSON.toJSONString(attribute));
        mutate.headers(x -> {
            x.remove(tokenHeader);
            x.remove(wetokenHeader);
        });
        ServerWebExchange newExchange = exchange.mutate().request(request).build();
        return chain.filter(newExchange);
    }

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

Spring Cloud Gateway 3 和 Spring WebFlux 中获取请求ip

@Slf4j
public class IPAddressUtil {

    public static final String LOCALHOST_IPV4 = "127.0.0.1";

    public static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";

    private static final String UNKNOWN = "unknown";

    public static String getIpAddress(ServerHttpRequest req) {
        String ip = IPAddressUtil.getFirst(req.getHeaders().get("x-forwarded-for"));
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            InetSocketAddress remoteAddress = req.getRemoteAddress();
            if (remoteAddress != null) {
                ip = remoteAddress.getHostString();
            }
        }
        String comma = ",";
        if (ip.contains(comma)) {
            ip = ip.split(",")[0];
        }
        if (LOCALHOST_IPV4.equals(ip) || LOCALHOST_IPV6.equals(ip)) {
            // 获取本机真正的ip地址,内部ip
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
        }
        return ip;
    }

    public static String getFirst(List<String> list) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        return list.get(0);
    }
}

持续更新中…
欢迎大家在评论区补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值