网关做加解密处理解决方案

1.定义一个请求过滤器,之前遇到的坑,请求参数传不到业务层,两次请求会报一次400异常。

package com.qiyee.job.common.filter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiyee.job.common.configure.JobGatewayProperties;
import com.qiyee.job.common.utils.AesUtil;
import com.qiyee.job.common.utils.RsaUtil;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;

import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerStrategies;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Objects;

/**
 * @program: qiyee-job-gateway
 * @description: 过滤器重写
 * @author: 石亚宁
 * @create: 2021-03-25 16:46
 */
@Slf4j
@Component
@Order(2)
public class RequestEncryptionGlobalFilter implements GlobalFilter, Ordered {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private JobGatewayProperties jobGatewayProperties;
    private AntPathMatcher pathMatcher = new AntPathMatcher();
    @Override
    public int getOrder() {
        return -2;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return processRequest(exchange, chain);
    }
    /**
     *
     *
     * @param request
     * @return true 需要解密 false不需要解密
     */
    private Boolean checkDecRequestUri(ServerHttpRequest request) {

        String uri = request.getPath().toString();
        String method = request.getMethodValue().toLowerCase();
        boolean shouldDes = false;
        if (!"post".equals(method)){
            return false;
        }
        List<String> rsaRequestUrl = jobGatewayProperties.getRsaRequestUrl();
        /**
         * 不需要解密
         */
        if (!ResponseDecryptionGlobalFilter.checkVsersion(request, jobGatewayProperties)){
            return false;
        }
        if (!Collections.isEmpty(rsaRequestUrl)) {
            for (String u : rsaRequestUrl) {
                if (pathMatcher.match(uri,u)) {
                    shouldDes = true;
                    break;
                }
            }
        }


        return shouldDes;
    }
    private Mono<Void> processRequest(ServerWebExchange exchange, GatewayFilterChain chain) {
        //不需要解密
        ServerHttpRequest request=exchange.getRequest();
        if (!checkDecRequestUri(request)){
            return chain.filter(exchange);
        }
        if (exchange.getRequest().getMethod().equals(HttpMethod.POST)) {
            //重新构造request,参考ModifyRequestBodyGatewayFilterFactory
            ServerRequest serverRequest = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
            MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
            //重点
            Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
                //因为约定了终端传参的格式,所以只考虑json的情况,如果是表单传参,请自行发挥
               
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值