//package com.bonus.gateway.filter;
//
//import com.bonus.common.core.exception.CaptchaException;
//import com.bonus.common.core.utils.StringUtils;
//import com.bonus.common.core.utils.encryption.AesCbcUtils;
//import com.bonus.common.core.utils.encryption.Sm3Util;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//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.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.util.UriComponentsBuilder;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.net.URI;
//import java.nio.charset.StandardCharsets;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Component
//public class AecDecryptParamFilter extends AbstractGatewayFilterFactory {
//
//    public boolean decryptEnabled = true;
//
//    public static final String HEADER_NAME = "decrypt";
//    public static final String HMAC_HEADER_NAME = "Params-Hash";
//
//    @Override
//    public GatewayFilter apply(Object config) {
//        return (exchange, chain) -> {
//            if (!decryptEnabled) {
//                log.info("解密功能已禁用，直接继续过滤链。");
//                return chain.filter(exchange);
//            }
//            ServerHttpRequest request = exchange.getRequest();
//            boolean hasContentHeader = request.getHeaders().containsKey("Content-Type");
//            if (hasContentHeader) {
//                return handlePostPutRequest(exchange, chain);
//            } else {
//                return handleGetRequest(exchange, chain);
//            }
//        };
//    }
//
//    private Mono<Void> handleGetRequest(ServerWebExchange exchange, GatewayFilterChain chain) {
//        try {
//            ServerWebExchange updatedExchange = updateRequestParam(exchange);
//            if (updatedExchange != null) {
//                return chain.filter(updatedExchange);
//            } else {
//                return chain.filter(exchange);
//            }
//        } catch (Exception e) {
//            log.error("处理 GET 请求时发生错误: {}", e.getMessage(), e);
//            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
//            return exchange.getResponse().setComplete();
//        }
//    }
//
//    private Mono<Void> handlePostPutRequest(ServerWebExchange exchange, GatewayFilterChain chain) {
//        return DataBufferUtils.join(exchange.getRequest().getBody())
//                .flatMap(dataBuffer -> {
//                    byte[] body = new byte[dataBuffer.readableByteCount()];
//                    dataBuffer.read(body);
//                    DataBufferUtils.release(dataBuffer);
//
//                    String requestBody = new String(body, StandardCharsets.UTF_8);
//                    // 去掉多余的引号（如果有）
//                    if (requestBody.startsWith("\"") && requestBody.endsWith("\"")) {
//                        requestBody = requestBody.substring(1, requestBody.length() - 1);
//                    }
//
//                    String decryptedBody;
//                    try {
//                        String providedHmac = exchange.getRequest().getHeaders().getFirst(HMAC_HEADER_NAME);
//                        integrityVerification(providedHmac, requestBody);
//                        decryptedBody = AesCbcUtils.decrypt(requestBody);
//                    } catch (Exception e) {
//                        log.error("解密请求体时发生错误: {}", e.getMessage(), e);
//                        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
//                        return exchange.getResponse().setComplete();
//                    }
//
//                    DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
//                    DataBuffer newBody = bufferFactory.wrap(decryptedBody.getBytes(StandardCharsets.UTF_8));
//                    ServerHttpRequest newRequest = createNewRequest(exchange, newBody);
//
//                    return chain.filter(exchange.mutate().request(newRequest).build());
//                });
//    }
//
//    private ServerHttpRequest createNewRequest(ServerWebExchange exchange, DataBuffer newBody) {
//        return new ServerHttpRequestDecorator(exchange.getRequest()) {
//            @Override
//            public Flux<DataBuffer> getBody() {
//                return Flux.just(newBody);
//            }
//
//            @Override
//            public HttpHeaders getHeaders() {
//                HttpHeaders headers = new HttpHeaders();
//                headers.putAll(exchange.getRequest().getHeaders());
//                headers.remove(HttpHeaders.CONTENT_LENGTH);
//                headers.setContentLength(newBody.readableByteCount());
//                return headers;
//            }
//        };
//    }
//
//    private ServerWebExchange updateRequestParam(ServerWebExchange exchange) throws NoSuchFieldException, IllegalAccessException {
//        ServerHttpRequest request = exchange.getRequest();
//        URI uri = request.getURI();
//        String query = uri.getQuery();
//        if (StringUtils.isNotBlank(query)) {
//            String providedHmac = exchange.getRequest().getHeaders().getFirst(HMAC_HEADER_NAME);
//            integrityVerification(providedHmac, query);
//            String decryptedParam;
//            try {
//                decryptedParam = AesCbcUtils.decrypt(query);
//            } catch (Exception e) {
//                log.error("解密查询参数时发生错误: {}", e.getMessage(), e);
//                throw new CaptchaException("请求参数不正确");
//            }
//            if (StringUtils.isEmpty(decryptedParam)) {
//                return null;
//            }
//            Map<String, List<String>> queryParams = Arrays.stream(decryptedParam.split("&"))
//                    .map(param -> param.split("="))
//                    .collect(Collectors.toMap(
//                            param -> param[0],
//                            param -> Collections.singletonList(param[1])
//                    ));
//            URI newUri = UriComponentsBuilder.fromUri(uri)
//                    .replaceQueryParams(CollectionUtils.toMultiValueMap(queryParams))
//                    .build(true)
//                    .toUri();
//
//            ServerHttpRequest newRequest = request.mutate().uri(newUri).build();
//
//            return exchange.mutate().request(newRequest).build();
//        }
//        return null;
//    }
//
//    /**
//     * 数据完整性校验
//     *
//     * @param providedHmac 请求头中的 HMAC 值
//     * @param query        请求参数
//     */
//    private void integrityVerification(String providedHmac, String query) {
//       /* if (providedHmac == null) {
//            log.error("请求头中缺少 Params-Hash");
//            throw new CaptchaException("请求参数不正确");
//        }
//        String encrypt = Sm3Util.encrypt(query);
//        log.debug("加密后的参数: {}", encrypt);
//        log.debug("请求头中的 Params-Hash: {}", providedHmac);
//        if (!encrypt.equals(providedHmac)) {
//            log.error("参数校验失败");
//            throw new CaptchaException("请求参数不正确");
//        }*/
//    }
//}
