选用Spring Cloud Gateway进行一些业务操作

选用Spring Cloud Gateway进行一些业务操作

选用spring cloud gateway作为api网关,实现路由转发、访问控制、流量染色、集中处理签名校验、请求参数校验、接口调用统计等业务逻辑,提高安全性的同时,便于系统开发维护。

  • 为什么要选用spring cloud gateway作为网关

因为spring cloud gateway有更优秀的性能和更强大的功能。例如安全认证,限流,监控。

工作原理:

img

拦截到对应路由的请求,转发到网关的web处理,经过特定的过滤链路,最后被转发到指定服务,过滤链路前后可以执行不同的逻辑。

  • 怎么实现的路由转发
spring:
	cloud:
		gateway:
			default-filters:
				- AddResponseHeader=source, xc
        - AddRequestHeader=gatewayKey,xcxc
			routes:
				- id: api_route
					uri: http://localhost:8123
# 配置断言,符合此断言的请求将会被转发到指定地址
					predicates:
						- Path=/api/**
  • 怎么实现的访问控制

简单来说就是配置黑/白名单,来对某个源ip地址的请求拒绝/允许

/**
 * 全局过滤
 * @author xc
 */
@Slf4j
@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    // 1. 配置白名单,只允许127.0.0.1的源ip地址访问网关
    private static final List<String> IP_WHITE_LIST = Arrays.asList("127.0.0.1");
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String sourceAddress = request.getLocalAddress().getHostString();
        // 2. 访问控制 - 黑白名单
        if (!IP_WHITE_LIST.contains(sourceAddress)) {
            response.setStatusCode(HttpStatus.FORBIDDEN);
            return response.setComplete();
        }
        return handleResponse(exchange, chain, interfaceInfo.getId(), invokeUser.getId());
    }

    public Mono<Void> handleResponse(ServerWebExchange exchange, GatewayFilterChain chain, long interfaceInfoId, long userId) {}
  • 怎么实现的流量染色

在配置文件的filters中添加请求头

spring:
  cloud:
    gateway:
			routes:
				- id: api_route
					uri: http://localhost:8123
        filters:
# 添加请求头,在下游判断有没有带上此请求头,没有说明没有经过网关
        - AddRequestHeader=gatewayKey,xcxc
  • 怎么实现的集中处理签名校验

在filter中处理

    // 3. 用户鉴权(判断 ak、sk 是否合法)
    HttpHeaders headers = request.getHeaders();
    String accessKey = headers.getFirst("accessKey");
    String nonce = headers.getFirst("nonce");
    String timestamp = headers.getFirst("timestamp");
    String sign = headers.getFirst("sign");
    String body = null;
    try {
        body = URLDecoder.decode(headers.getFirst("body"),"utf-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    User invokeUser = null;
    try {
        invokeUser = innerUserService.getInvokeUser(accessKey);
    } catch (Exception e) {
        log.error("getInvokeUser error", e);
    }
    if (invokeUser == null) {
        return handleNoAuth(response);
    }
    // 如果两次签名不一样,则拒绝此次请求
    String genSign = SignUtils.genSign(body, invokeUser.getSecretKey());
    if (!Objects.equals(genSign, sign)) {
        return handleNoAuth(response);
    }
    
    if (Long.parseLong(nonce) > 10000L) {
        return handleNoAuth(response);
    }
    // 时间和当前时间不能超过 1 分钟
    Long currentTime = System.currentTimeMillis() / 1000;
    final Long ONE_MINUTES = 60 * 1L;
    if ((currentTime - Long.parseLong(timestamp)) >= ONE_MINUTES) {
        return handleNoAuth(response);
    }
    // nonce不能存在redis,否则视为重放
    if (redisTemplate.hasKey(nonce)) {
        return handleNoAuth(response);
    }
    
    // 将nonce和timestamp写入redis
    // redis缓存时间要略大于时间戳设置的时间
    redisTemplate.boundValueOps(nonce).set(nonce,2, TimeUnit.MINUTES);
    // 实际情况中是从数据库中查出 secretKey
    String secretKey = invokeUser.getSecretKey();
    String serverSign = SignUtils.genSign(body, secretKey);
    if (sign == null || !sign.equals(serverSign)) {
        return handleNoAuth(response);
    }
  • 怎么实现接口调用统计

编写处理响应方法,对成功调用接口过后,统计次数加一

 /**
     * 处理响应
     *
     * @param exchange
     * @param chain
     * @return
     */
    public Mono<Void> handleResponse(ServerWebExchange exchange, GatewayFilterChain chain, long interfaceInfoId, long userId) {
        try {
            ServerHttpResponse originalResponse = exchange.getResponse();
            // 缓存数据的工厂
            DataBufferFactory bufferFactory = originalResponse.bufferFactory();
            // 拿到响应码
            HttpStatus statusCode = originalResponse.getStatusCode();
            if (statusCode == HttpStatus.OK) {
                // 装饰,增强能力
                ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
                    // 等调用完转发的接口后才会执行
                    @Override
                    public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                        log.info("body instanceof Flux: {}", (body instanceof Flux));
                        if (body instanceof Flux) {
                            Flux<? extends DataBuffer> fluxBody = Flux.from(body);
                            // 往返回值里写数据
                            // 拼接字符串
                            return super.writeWith(
                                    fluxBody.map(dataBuffer -> {
                                        // 7. 调用成功,接口调用次数 + 1 invokeCount
                                        try {
                                            innerUserInterfaceInfoService.invokeCount(interfaceInfoId, userId);
                                        } catch (Exception e) {
                                            log.error("invokeCount error", e);
                                        }
                                        byte[] content = new byte[dataBuffer.readableByteCount()];
                                        dataBuffer.read(content);
                                        DataBufferUtils.release(dataBuffer);//释放掉内存
                                        // 构建日志
                                        StringBuilder sb2 = new StringBuilder(200);
                                        List<Object> rspArgs = new ArrayList<>();
                                        rspArgs.add(originalResponse.getStatusCode());
                                        String data = new String(content, StandardCharsets.UTF_8); //data
                                        sb2.append(data);
                                        // 打印日志
                                        log.info("响应结果:" + data);
                                        return bufferFactory.wrap(content);
                                    }));
                        } else {
                            // 8. 调用失败,返回一个规范的错误码
                            log.error("<--- {} 响应code异常", getStatusCode());
                        }
                        return super.writeWith(body);
                    }
                };
                // 设置 response 对象为装饰过的
                return chain.filter(exchange.mutate().response(decoratedResponse).build());
            }
            return chain.filter(exchange); // 降级处理返回数据
        } catch (Exception e) {
            log.error("网关处理响应异常" + e);
            return chain.filter(exchange);
        }
    }
       }
            return chain.filter(exchange); // 降级处理返回数据
        } catch (Exception e) {
            log.error("网关处理响应异常" + e);
            return chain.filter(exchange);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值