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);
        }
相关推荐
2401_8576100310 分钟前
Spring Boot框架:电商系统的技术优势
java·spring boot·后端
希忘auto26 分钟前
详解MySQL安装
java·mysql
yaosheng_VALVE30 分钟前
稀硫酸介质中 V 型球阀的材质选择与选型要点-耀圣
运维·spring cloud·自动化·intellij-idea·材质·1024程序员节
冰淇淋烤布蕾37 分钟前
EasyExcel使用
java·开发语言·excel
拾荒的小海螺43 分钟前
JAVA:探索 EasyExcel 的技术指南
java·开发语言
Jakarta EE1 小时前
正确使用primefaces的process和update
java·primefaces·jakarta ee
马剑威(威哥爱编程)1 小时前
哇喔!20种单例模式的实现与变异总结
java·开发语言·单例模式
java—大象1 小时前
基于java+springboot+layui的流浪动物交流信息平台设计实现
java·开发语言·spring boot·layui·课程设计
杨哥带你写代码2 小时前
网上商城系统:Spring Boot框架的实现
java·spring boot·后端
camellias_2 小时前
SpringBoot(二十一)SpringBoot自定义CURL请求类
java·spring boot·后端