屏蔽词过滤器 2

简介: 屏蔽词过滤器

屏蔽词过滤器 1:https://developer.aliyun.com/article/1379261

然后是过滤器中需要的过滤请求的封装好的request

package com.ruben.simplescaffold.filter.wrappers;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruben.simplescaffold.utils.sensitive.SensitiveWordUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
 * 封装请求
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/8/7 21:50
 */
public class RequestWrapper extends HttpServletRequestWrapper {
    private final Map<String, String[]> parameterMap;
    private final byte[] body;
    public RequestWrapper(HttpServletRequest request, SensitiveWordUtils sensitiveWordUtils, Map<String, String[]> parameterMap, String bodyString) {
        super(request);
        if (sensitiveWordUtils == null) {
            this.parameterMap = Optional.ofNullable(parameterMap).orElseGet(super::getParameterMap);
            queryStringPutParameterMap(super.getQueryString());
            this.body = Optional.ofNullable(bodyString).orElseGet(() -> BodyHelper.getBodyString(request)).getBytes(StandardCharsets.UTF_8);
        } else {
            String paramString = JSON.toJSONString(Optional.ofNullable(parameterMap).orElseGet(super::getParameterMap));
            paramString = sensitiveWordUtils.replaceSensitiveWord(paramString, SensitiveWordUtils.MAX_MATCH_TYPE, SensitiveWordUtils.REPLACE_CHAR);
            this.parameterMap = JSON.parseObject(paramString, new TypeReference<Map<String, String[]>>() {
            });
            Optional.ofNullable(super.getQueryString()).ifPresent(queryString -> queryStringPutParameterMap(sensitiveWordUtils.replaceSensitiveWord(queryString, SensitiveWordUtils.MAX_MATCH_TYPE, SensitiveWordUtils.REPLACE_CHAR)));
            this.body = sensitiveWordUtils.replaceSensitiveWord(Optional.ofNullable(bodyString).orElseGet(() -> BodyHelper.getBodyString(request)), SensitiveWordUtils.MAX_MATCH_TYPE, SensitiveWordUtils.REPLACE_CHAR).getBytes(StandardCharsets.UTF_8);
        }
    }
    @Override
    public String getParameter(String name) {
        String[] values = parameterMap.get(name);
        if (Objects.isNull(values) || values.length == 0) {
            return null;
        }
        return values[0];
    }
    @Override
    public Map<String, String[]> getParameterMap() {
        if (parameterMap == null) {
            return super.getParameterMap();
        }
        return parameterMap;
    }
    @Override
    public Enumeration<String> getParameterNames() {
        if (parameterMap == null) {
            return super.getParameterNames();
        }
        return new Vector<>(parameterMap.keySet()).elements();
    }
    @Override
    public String[] getParameterValues(String name) {
        if (parameterMap == null) {
            return super.getParameterValues(name);
        }
        return parameterMap.get(name);
    }
    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
        return new ServletInputStream() {
            @Override
            public int read() {
                return byteArrayInputStream.read();
            }
            @Override
            public boolean isFinished() {
                return false;
            }
            @Override
            public boolean isReady() {
                return false;
            }
            @Override
            public void setReadListener(ReadListener arg0) {
            }
        };
    }
    @Override
    public String getHeader(String name) {
        return super.getHeader(name);
    }
    @Override
    public Enumeration<String> getHeaderNames() {
        return super.getHeaderNames();
    }
    @Override
    public Enumeration<String> getHeaders(String name) {
        return super.getHeaders(name);
    }
    private void queryStringPutParameterMap(String queryString) {
        if (queryString != null && queryString.trim().length() > 0) {
            String[] params = queryString.split("&");
            for (String param : params) {
                int splitIndex = param.indexOf("=");
                if (splitIndex == -1) {
                    continue;
                }
                String key = param.substring(0, splitIndex);
                if (!this.parameterMap.containsKey(key)) {
                    if (splitIndex < param.length()) {
                        String value = param.substring(splitIndex + 1);
                        this.parameterMap.put(key, new String[]{value});
                    }
                }
            }
        }
    }
    public static class BodyHelper {
        /**
         * 获取请求Body
         *
         * @param request
         * @return
         */
        public static String getBodyString(ServletRequest request) {
            StringBuilder sb = new StringBuilder();
            InputStream inputStream = null;
            BufferedReader reader = null;
            try {
                inputStream = request.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return sb.toString();
        }
    }
}

还有个获取响应结果的封装好的response,虽然此处我们没用到,但如果我们要获取filter中的响应体执行过滤,也可以用这个

package com.ruben.simplescaffold.filter.wrappers;
import lombok.SneakyThrows;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
/**
 * 过滤器获取响应结果
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/7/29 10:13
 */
public class ResponseWrapper extends HttpServletResponseWrapper {
    private ByteArrayOutputStream buffer = null;
    private ServletOutputStream outputStream = null;
    private PrintWriter writer = null;
    /**
     * Constructs a response adaptor wrapping the given response.
     *
     * @param response The response to be wrapped
     * @throws IllegalArgumentException if the response is null
     */
    @SneakyThrows
    public ResponseWrapper(HttpServletResponse response) {
        super(response);
        buffer = new ByteArrayOutputStream();
        outputStream = new WrapperOutputStream(buffer);
        writer = new PrintWriter(new OutputStreamWriter(buffer, StandardCharsets.UTF_8));
    }
    /**
     * The default behavior of this method is to return getOutputStream() on the
     * wrapped response object.
     */
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return outputStream;
    }
    /**
     * The default behavior of this method is to return getWriter() on the
     * wrapped response object.
     */
    @Override
    public PrintWriter getWriter() throws IOException {
        return writer;
    }
    /**
     * The default behavior of this method is to call flushBuffer() on the
     * wrapped response object.
     */
    @Override
    @SneakyThrows
    public void flushBuffer() {
        if (outputStream != null) {
            outputStream.flush();
        }
        if (writer != null) {
            writer.flush();
        }
    }
    /**
     * The default behavior of this method is to call reset() on the wrapped
     * response object.
     */
    @Override
    public void reset() {
        buffer.reset();
    }
    /**
     * Get response content
     *
     * @param charset HttpServletResponse#getCharacterEncoding()
     * @return response content
     */
    @SneakyThrows
    public String getResponseData(String charset) {
        // 将out、writer中的数据强制输出到WrapperResponse的buffer里面,否则取不到数据
        flushBuffer();
        return buffer.toString(StandardCharsets.UTF_8.displayName());
    }
    /**
     * 内部类,对ServletOutputStream进行包装,指定输出流的输出端
     */
    private static class WrapperOutputStream extends ServletOutputStream {
        private ByteArrayOutputStream bos = null;
        public WrapperOutputStream(ByteArrayOutputStream stream) throws IOException {
            bos = stream;
        }
        /**
         * Writes the specified byte to this output stream. The general
         * contract for <code>write</code> is that one byte is written
         * to the output stream. The byte to be written is the eight
         * low-order bits of the argument <code>b</code>. The 24
         * high-order bits of <code>b</code> are ignored.
         * <p>
         * Subclasses of <code>OutputStream</code> must provide an
         * implementation for this method.
         *
         * @param b the <code>byte</code>.
         * @throws IOException if an I/O error occurs. In particular,
         *                     an <code>IOException</code> may be thrown if the
         *                     output stream has been closed.
         */
        @Override
        public void write(int b) throws IOException {
            bos.write(b);
        }
        /**
         * Checks if a non-blocking write will succeed. If this returns
         * <code>false</code>, it will cause a callback to
         * {@link WriteListener#onWritePossible()} when the buffer has emptied. If
         * this method returns <code>false</code> no further data must be written
         * until the contain calls {@link WriteListener#onWritePossible()}.
         *
         * @return <code>true</code> if data can be written, else <code>false</code>
         * @since Servlet 3.1
         */
        @Override
        public boolean isReady() {
            return false;
        }
        /**
         * Sets the {@link WriteListener} for this {@link ServletOutputStream} and
         * thereby switches to non-blocking IO. It is only valid to switch to
         * non-blocking IO within async processing or HTTP upgrade processing.
         *
         * @param listener The non-blocking IO write listener
         * @throws IllegalStateException If this method is called if neither
         *                               async nor HTTP upgrade is in progress or
         *                               if the {@link WriteListener} has already
         *                               been set
         * @throws NullPointerException  If listener is null
         * @since Servlet 3.1
         */
        @Override
        public void setWriteListener(WriteListener listener) {
        }
    }
}

最后是测试用的controller

package com.ruben.simplescaffold.controller.rest;
import com.ruben.simplescaffold.pojo.common.Result;
import com.ruben.simplescaffold.pojo.dto.CommonDTO;
import org.springframework.web.bind.annotation.*;
/**
 * 测试控制层
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/8/7 22:58
 */
@RestController
@RequestMapping("test")
public class TestController {
    @PostMapping
    public Result testBody(@RequestBody CommonDTO commonDTO) {
        return Result.ok().data(commonDTO);
    }
    @GetMapping
    public Result testQueryParam(CommonDTO commonDTO) {
        return Result.ok().data(commonDTO);
    }
    @GetMapping("{path}")
    public Result testPath(@PathVariable String path) {
        return Result.ok().data(path);
    }
}

这里还用到两个类,一个响应结果Result,一个CommonDTO,CommonDTO大伙可以使用Map代替下

package com.ruben.simplescaffold.pojo.common;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;
import java.util.HashMap;
import java.util.Optional;
/**
 * 自定义返回响应
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/6/23 10:40
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(fluent = true)
@EqualsAndHashCode(callSuper = true)
public class Result extends HashMap<String, Object> {
    public static final String DATA_KEY = "data";
    private static final long serialVersionUID = 8639693790921600208L;
    /**
     * 成功状态
     */
    private Boolean success;
    /**
     * 消息
     */
    private String msg;
    /**
     * 状态码
     */
    private Integer code;
    public static Result ok() {
        return httpStatus(HttpStatus.OK);
    }
    public static Result error(String msg) {
        return httpStatus(HttpStatus.INTERNAL_SERVER_ERROR).msg(msg);
    }
    public static Result paramError() {
        return httpStatus(HttpStatus.BAD_REQUEST);
    }
    public static Result notLogin() {
        return httpStatus(HttpStatus.UNAUTHORIZED);
    }
    public static Result httpStatus(HttpStatus httpStatus) {
        httpStatus = Optional.ofNullable(httpStatus).orElse(HttpStatus.OK);
        HttpStatus.Series series = httpStatus.series();
        return new Result().success(httpStatus.is2xxSuccessful()).msg(series.name()).code(HttpStatus.OK.value());
    }
    /**
     * 放入key和value
     *
     * @param key   键
     * @param value 值
     * @return com.kuang.honghaisyweb.pojo.common.Result
     * @author <achao1441470436@gmail.com>
     * @since 2021/6/23 17:07
     */
    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }
    /**
     * 返回数据
     *
     * @param value 数据
     * @return com.kuang.honghaisyweb.pojo.common.Result
     * @author <achao1441470436@gmail.com>
     * @since 2021/6/23 17:07
     */
    public Result data(Object value) {
        this.put(DATA_KEY, value);
        return this;
    }
}

最后是屏蔽词文本

屏蔽词1号
屏蔽词2号
屏蔽词3号

以及测试案例:

可以看到body和param传参都替换成了*号,url的path传参也替换成了0

完整源码:https://gitee.com/VampireAchao/simple-scaffold.git

相关文章
N..
|
安全 API 开发工具
如何升级Flutter
如何升级Flutter
N..
1452 1
|
9月前
|
人工智能 安全 Linux
OpenClaw(Clawdbot)喂饭级指南:阿里云/本地部署、百炼API配置、核心Skill获取、实用技巧与避坑手册
OpenClaw(原Clawdbot,俗称“小龙虾”)作为2026年热门的开源AI Agent框架,其设计核心是“基础框架+第三方技能”的模块化架构——官方仅提供基础交互能力,真正的智能性需通过安装适配场景的Skills实现。多数用户部署后觉得“不够智能”,核心问题并非工具本身,而是未掌握技能拓展与使用技巧。
1496 1
|
10月前
|
人工智能 安全 数据安全/隐私保护
2026年阿里云部署OpenClaw(Clawdbot)一键接入iMessage喂饭级教程
2026年,OpenClaw(前身为Clawdbot、Moltbot)凭借轻量化容器化架构、灵活的跨平台集成能力,成为个人与小型团队打造专属AI助手的首选工具,其支持自然语言指令解析、多任务自动化执行、多终端同步响应的核心特性,完美适配苹果生态用户的办公与生活需求。iMessage作为苹果生态核心的即时通讯工具,覆盖iPhone、Mac、iPad全终端,具备端到端加密、无缝同步的优势,将OpenClaw与iMessage深度对接,可实现“iMessage发指令、AI自动化执行、全终端收结果”的跨场景体验,无需切换APP,随时随地调用AI能力处理文档生成、数据查询、任务提醒等事务。
853 1
|
数据采集 人工智能
Dataphin X-数据质量,智能分析质量问题并推荐整改建议
针对数据治理中质量问题难发现、根因定位难、整改效率低等痛点,推出AI驱动的数据质量问题智能分析功能。通过智能分析异常、追溯根因、构建数据证据链,自动生成含改进建议与影响评估的质量报告,提升治理效率与决策可靠性。
562 3
|
人工智能 编解码 芯片
告别低效沟通|让技术提问不再头疼-这套高效AI提问模板来帮你
不会向ai提问,不知道怎么提问的 可以看看
21632 1
告别低效沟通|让技术提问不再头疼-这套高效AI提问模板来帮你
|
传感器 人工智能 边缘计算
AI赋能油田巡检——无人机视频监控系统的技术解析
无人机油田巡检系统融合无人机硬件与AI视频监控技术,实现全域覆盖、智能分析和高效管理。通过多旋翼/固定翼无人机搭载高分辨率摄像头及传感器,采集多维数据;结合YOLOv9等算法进行异常检测,准确率高达98%。系统支持5G实时传输、边缘计算及集中化管理平台,提供可视化监控与预测性维护。基于开源框架设计,灵活扩展且成本低,大幅提升油田巡检效率与安全性。
1554 0
|
运维 安全 Linux
IDC服务器故障排除思路
本文详细介绍了服务器维修流程,包括维修前的工具和备件准备,以及不拆机情况下的初步检查步骤。文中还提供了拆机维修的具体方法,如最小化测试法、替换法和交叉比较法,并针对CPU、主板、内存、硬盘、电源、风扇、网卡及BMC等主要配件的故障排除进行了说明,强调了注意事项,旨在帮助技术人员快速准确地定位并解决问题。
1664 14
|
传感器 存储 人工智能
一文彻底搞清楚数字电路
数字电路是处理离散二进制信号(0和1)的电子电路,由逻辑门(如与门、或门等)组成,实现各种逻辑运算。它在计算机、通信、自动控制和数字信号处理等领域广泛应用。例如,CPU通过数字电路执行算术和逻辑运算,PLC用于工业自动化控制,数字滤波器则用于信号处理。数字电路以高电平(如5V)表示1,低电平(如0V)表示0,简化了信号处理并提高了系统的可靠性和抗干扰能力。
2621 0
一文彻底搞清楚数字电路
|
Kubernetes 前端开发 分布式数据库
工作中常见的软件系统部署架构
在实际应用中,会根据项目的具体需求、规模、性能要求等因素选择合适的部署架构,或者综合使用多种架构模式来构建稳定、高效、可扩展的系统。
2349 2
|
Kubernetes API 索引