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);
        }
相关推荐
陈王卜6 分钟前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、6 分钟前
Spring Boot 注解
java·spring boot
java亮小白199711 分钟前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF17 分钟前
java Queue 详解
java·队列
跳跳的向阳花18 分钟前
03-03、SpringCloud第三章,负载均衡Ribbon和Feign
spring cloud·ribbon·负载均衡
武子康39 分钟前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs
武子康41 分钟前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql
苏-言1 小时前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
界面开发小八哥1 小时前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base1 小时前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring