Information Security 12 min read

Guide to Using the Sensitive‑Word Java Library for Sensitive Word Detection and Replacement

This article provides a step‑by‑step tutorial on integrating the Sensitive‑Word Java library via Maven, demonstrates core find/replace APIs, showcases advanced detection features such as email, URL, IPv4 and custom replacement strategies, and includes complete code examples and output results.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Guide to Using the Sensitive‑Word Java Library for Sensitive Word Detection and Replacement

The document introduces the Sensitive‑Word library, a Java tool for detecting and handling sensitive words in text. It begins with the Maven dependency snippet required to add the latest version of the library to a project.

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>sensitive-word</artifactId>
    <version>0.18.0</version>
</dependency>

Next, it presents core usage examples, including basic find/replace operations, custom result handling, and detection of case‑insensitive, special‑character, numeric, and traditional‑simplified variations of sensitive words.

package com.example.demo;

import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordReplace;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.houbb.sensitive.word.support.result.WordResultHandlers;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;

import java.util.List;

public class SensitiveWordTestDemo {
    public static void main(String[] args) {
        testMoreFeatures();
    }

    // ... (other test methods omitted for brevity) ...

    public static void testMoreFeatures() {
        // 1. Email detection
        String text = "楼主好人,邮箱 [email protected]";
        List
wordList = SensitiveWordBs.newInstance()
            .enableEmailCheck(true).init().findAll(text);
        System.out.println("是否存在邮箱:" + wordList.toString());
        // 2. Continuous number detection
        text = "你懂得:12345678";
        List
wordList1 = SensitiveWordBs.newInstance()
            .enableNumCheck(true).init().findAll(text);
        System.out.println("是否存在连续数字字符串:" + wordList1);
        // 3. URL detection
        text = "点击链接 https://www.baidu.com 查看答案";
        SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
            .enableUrlCheck(true).init();
        List
wordList3 = sensitiveWordBs.findAll(text);
        System.out.println("是否存在网址信息:" + wordList3.toString());
        System.out.println("是否存在网址信息2并替换:" + sensitiveWordBs.replace(text));
        // 4. IPv4 detection
        text = "个人网站,如果网址打不开可以访问 127.0.0.1。";
        SensitiveWordBs sensitiveWordBs2 = SensitiveWordBs.newInstance()
            .enableIpv4Check(true).init();
        List
wordList4 = sensitiveWordBs2.findAll(text);
        System.out.println("是否存在 IPv4:" + wordList4.toString());
    }
}

Additional sections cover custom replacement strategies by implementing IWordReplace , allowing developers to replace specific sensitive words with alternative terms or mask them with asterisks.

class MySensitiveWordReplace implements IWordReplace {
    @Override
    public void replace(StringBuilder stringBuilder, char[] chars, IWordResult wordResult, IWordContext iWordContext) {
        String sensitiveWord = InnerWordCharUtils.getString(chars, wordResult);
        if ("五星红旗".equals(sensitiveWord)) {
            stringBuilder.append("国家旗帜");
        } else if ("毛主席".equals(sensitiveWord)) {
            stringBuilder.append("教员");
        } else {
            int wordLength = wordResult.endIndex() - wordResult.startIndex();
            for (int i = 0; i < wordLength; i++) {
                stringBuilder.append('*');
            }
        }
    }
}

The article also displays sample console output illustrating detection results, replacements with default asterisks, custom symbols, and the effects of the various detection options.

Finally, the open‑source repository URL (https://github.com/houbb/sensitive-word) is provided, along with a brief promotional note about a backend‑focused technical community.

backendJavaMavensecurityText FilteringSensitive Word
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.