//package com.dxhy.common.filter;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.IOUtils;
//
//import javax.servlet.ReadListener;
//import javax.servlet.ServletInputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequestWrapper;
//import java.io.IOException;
//
///**
// * @ClassName SqlInjectHttpServletRequestWrapper
// * @Description TODO
// * @Author Allen
// * @Date 2023/6/13 15:37
// * @Version 1.0
// */
//@Slf4j
//public class SqlInjectHttpServletRequestWrapper extends HttpServletRequestWrapper {
//    private final byte[] body;
//
//    /**
//     * 构造请求对象
//     *
//     * @param request
//     */
//    public SqlInjectHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
//        super(request);
//        body = IOUtils.toByteArray(super.getInputStream());
//    }
//
//    public byte[] getInputstrem() {
//        return body;
//    }
//
////    /**
////     * 获取头部参数
////     *
////     * @param v 参数值
////     */
////    @Override
////    public String getHeader(String v) {
////        String header = super.getHeader(v);
////        if (header == null || "".equals(header)) {
////            return header;
////        }
////        return sqlFilter(header);
////    }
//
//    @Override
//    public ServletInputStream getInputStream() throws IOException {
//        return new RequestBodyCachingInputStream(body);
//    }
//
//    private class RequestBodyCachingInputStream extends ServletInputStream {
//        private byte[] body;
//        private int lastIndexRetrieved = -1;
//        private ReadListener listener;
//
//        public RequestBodyCachingInputStream(byte[] body) {
//            this.body = body;
//        }
//
//        @Override
//        public int read() throws IOException {
//            if (isFinished()) {
//                return -1;
//            }
//            int i = body[lastIndexRetrieved + 1];
//            lastIndexRetrieved++;
//            if (isFinished() && listener != null) {
//                try {
//                    listener.onAllDataRead();
//                } catch (IOException e) {
//                    listener.onError(e);
//                    throw e;
//                }
//            }
//            return i;
//        }
//
//        @Override
//        public boolean isFinished() {
//            return lastIndexRetrieved == body.length - 1;
//        }
//
//        @Override
//        public boolean isReady() {
//            return isFinished();
//        }
//
//        @Override
//        public void setReadListener(ReadListener listener) {
//            if (listener == null) {
//                throw new IllegalArgumentException("listener cann not be null");
//            }
//            if (this.listener != null) {
//                throw new IllegalArgumentException("listener has been set");
//            }
//            this.listener = listener;
//            if (!isFinished()) {
//                try {
//                    listener.onAllDataRead();
//                } catch (IOException e) {
//                    listener.onError(e);
//                }
//            } else {
//                try {
//                    listener.onAllDataRead();
//                } catch (IOException e) {
//                    listener.onError(e);
//                }
//            }
//        }
//
//        @Override
//        public int available() throws IOException {
//            return body.length - lastIndexRetrieved - 1;
//        }
//
//        @Override
//        public void close() throws IOException {
//            lastIndexRetrieved = body.length - 1;
//            body = null;
//        }
//    }
//
//
////
////    /**
////     * 获取参数
////     *
////     * @param v 参数值
////     */
////    @Override
////    public String getParameter(String v) {
////        String param = super.getParameter(v);
////        if (param == null || "".equals(param)) {
////            return param;
////        }
////        return sqlFilter(param);
////    }
////
////    /**
////     * 获取参数值
////     *
////     * @param v 参数值
////     */
////    @Override
////    public String[] getParameterValues(String v) {
////        String[] values = super.getParameterValues(v);
////        if (values == null) {
////            return values;
////        }
////
////        int length = values.length;
////        String[] resultValues = new String[length];
////        for (int i = 0; i < length; i++) {
////            // 过滤特殊字符
////            resultValues[i] = sqlFilter(values[i]);
////            if (!(resultValues[i]).equals(values[i])) {
////                log.debug("SQL注入过滤器 => 过滤前：{} => 过滤后：{}", values[i], resultValues[i]);
////            }
////        }
////        return resultValues;
////    }
////
////    /**
////     * 预编译SQL过滤正则表达式
////     */
////    private Pattern sqlPattern = Pattern.compile(
////            "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)",
////            Pattern.CASE_INSENSITIVE);
////
////    /**
////     * SQL过滤
////     *
////     * @param v 参数值
////     * @return
////     */
////    private String sqlFilter(String v) {
////        if (v != null) {
////            String resultVal = v;
////            Matcher matcher = sqlPattern.matcher(resultVal);
////            if (matcher.find()) {
////                resultVal = matcher.replaceAll("");
////            }
////            if (!resultVal.equals(v)) {
////                return "";
////            }
////            return resultVal;
////        }
////        return null;
////    }
//}
