SpringCloud-Gateway网关实现入参统一解密

1.添加依赖

剩下的依赖自行添加...

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

2.创建配置文件

这里我使用了Nacos,使用时需要添加Nacos依赖 及配置

server:
  port: 8081

spring:
  application:
    name: itvgetway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: smsinterface
          uri: lb://smsinterfacecloud
          predicates:
            - Path=/smsinterface/**
          filters:
           - RequestSign

3.过滤器

直接看代码

java 复制代码
package com.example.itvgetway.filter;

import com.alibaba.fastjson.JSONObject;
import com.example.itvcommoncore.commons.AESCoder;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import java.net.URLEncoder;
import java.net.URI;
import java.nio.charset.StandardCharsets;


@Component
public class RequestSignGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    @Override
    public GatewayFilter apply(Object config) {
        return  new RequestSignGatewayFilter();
    }


    public class RequestSignGatewayFilter implements GatewayFilter, Ordered {


        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            //获取请求参数
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
            //拿到请求参数,我这里入参格式是:  http://ip:port?REQUEST=xxxxxxxx
            String decryptedParam = queryParams.getFirst("REQUEST");
            //使用解密方法解密
            String decryptedValue = decrypt(decryptedParam);
            // 编码参数值
            String encodedDecryptedParam = URLEncoder.encode(decryptedValue, StandardCharsets.UTF_8);
            // 构建修改后的URI
            URI modifiedUri = UriComponentsBuilder.fromUri(exchange.getRequest().getURI())
                    .replaceQueryParam("REQUEST", encodedDecryptedParam)
                    .build(true)
                    .toUri();
            // 修改请求,设置新的URI
            ServerHttpRequest modifiedRequest = exchange.getRequest().mutate()
                    .uri(modifiedUri)
                    .build();
            // 用修改后的请求继续处理请求链
            return chain.filter(exchange.mutate().request(modifiedRequest).build());

        }

        @Override
        public int getOrder() {
            return 1;
        }
    }
    //解密的方法
    private String decrypt(String encryptedParam) {
        String returnResult = "";
        JSONObject json = JSONObject.parseObject(encryptedParam);
        String encrypt = json.getString("ENCRYPT");
        try {
            returnResult = AESCoder.decryptAES(encrypt, "F51riKYgKK8PRrt+5IC6CQ==");
            return returnResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnResult;
    }
}

4.接口层

java 复制代码
@RestController
@RequestMapping("smsinterface")
public class Test{

    @GetMapping("hello")
    public String test(@RequestParam("REQUEST")String result){
        System.out.println(result);
        return result;
        
        }

}

5. 测试

我这里是编写了一个测试类

java 复制代码
 public static void main(String[] args) throws Exception {
        try {
            JSONObject jo = new JSONObject();
            jo.put("param1", "测试啊");
            jo.put("param2", "1");
            JSONObject reqjo = new JSONObject();
            String encrypt = "";
            //加密请求参数
            encrypt = AESCoder.encrypt(jo.toString(), "F51riKYgKK8PRrt+5IC6CQ==");
            reqjo.put("ENCRYPT", encrypt);
            reqjo.put("SUMMARY", MD5.GetMD5Code(encrypt));
            String responseBody = "";
            // 请求参数
                String params = "REQUEST="
                        + URLEncoder.encode(reqjo.toString(), "UTF-8");
            String localUrl="htp://localhost:8081/smsinterface/hello"

                HttpClientCommon hc = new HttpClientCommon();
                responseBody = hc.responseBody(localUrl + "?" + params, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(responseBody);
            JSONObject json = JSONObject.fromObject(responseBody);
            String res = (String) json.get("RESPONSE=");
            res = URLDecoder.decode(res, "UTF-8");
            byte[] bytes = AESCoder.decrypt(res, "F51riKYgKK8PRrt+5IC6CQ==");
            String returnResult = new String(bytes, "UTF-8");
            System.out.println("返回参数:" + returnResult);
        }
相关推荐
秋意钟1 分钟前
Spring新版本
java·后端·spring
椰椰椰耶3 分钟前
【文档搜索引擎】缓冲区优化和索引模块小结
java·spring·搜索引擎
mubeibeinv5 分钟前
项目搭建+图片(添加+图片)
java·服务器·前端
青莳吖6 分钟前
Java通过Map实现与SQL中的group by相同的逻辑
java·开发语言·sql
Buleall13 分钟前
期末考学C
java·开发语言
重生之绝世牛码15 分钟前
Java设计模式 —— 【结构型模式】外观模式详解
java·大数据·开发语言·设计模式·设计原则·外观模式
小蜗牛慢慢爬行21 分钟前
有关异步场景的 10 大 Spring Boot 面试问题
java·开发语言·网络·spring boot·后端·spring·面试
新手小袁_J1 小时前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅1 小时前
C#关键字volatile
java·redis·c#
Monly211 小时前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat