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

1.添加依赖

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

    
            org.springframework.cloud
            spring-cloud-starter-gateway
        

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.过滤器

直接看代码

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 {
    @Override
    public GatewayFilter apply(Object config) {
        return  new RequestSignGatewayFilter();
    }


    public class RequestSignGatewayFilter implements GatewayFilter, Ordered {


        @Override
        public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            //获取请求参数
MultiValueMap 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.接口层

@RestController
@RequestMapping("smsinterface")
public class Test{

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

}

5. 测试

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

 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);
        }

你可能感兴趣的:(spring,cloud,gateway,java)