Skip to content
广告❤️成为赞助商

Keyword Filtering

Mongo-Plus provides keyword filtering functionality to prevent users from saving prohibited or sensitive words, making development easier. Starting from version 2.1.9, Mongo-Plus supports filtering keywords via annotations or globally.

Note

When Mongo-Plus detects a sensitive word, it will throw a SensitiveWordException. You can catch this exception to get the sensitive word content. Replacement is not supported for now.

Dependency

Keyword automatic filtering is implemented by including mongo-plus-sensitive-word.

xml
<dependencyManagement>
    <dependencies>
        <!-- mongoplus-bom -->
        <dependency>
            <groupId>com.mongoplus</groupId>
            <artifactId>mongo-plus-bom</artifactId>
            <version>latest-version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- mongoplus keyword dependency -->
    <dependency>
        <groupId>com.mongoplus</groupId>
        <artifactId>mongo-plus-sensitive-word</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Configuration

MongoPlus supports two filtering methods: local and global. Local filtering requires the use of @SensitiveWord annotation; global filtering does not require annotations. Note that global filtering is less efficient, use with caution.

yaml
mongo-plus:
  sensitive-word:
    # Sensitive word check type, default LOCAL
    sensitive-type: LOCAL
    # Ignore case
    ignore-case: true
    # Ignore full-width and half-width characters
    ignore-width: true
    # Ignore numeric style
    ignore-num-style: true
    # Ignore Chinese style
    ignore-chinese-style: true
    # Ignore English style
    ignore-english-style: true
    # Ignore repeated characters
    ignore-repeat: false
    # Ignore special characters
    ignore-char: false
    # Enable consecutive number check
    enable-num-check: false
    # Enable email check
    enable-email-check: false
    # Enable URL check
    enable-url-check: false
    # Enable IPv4 check
    enable-ipv4-check: false

Example

For local validation type, you need to use the @SensitiveWord annotation, which can only be applied to entity class fields.

java
@CollectionName("user")
public class User {
    // Apply sensitive word filtering to the field
    @SensitiveWord
    private String userName;
}

Loading Sensitive Words

MongoPlus provides interfaces to dynamically load and unload sensitive words, allowing custom sensitive words to be loaded.

java
// Register as Bean
@Component
public class CustomLoadExtraWord implements LoadExtraWord {

    @Override
    public List<String> deny() {
        return List.of("Data that should be denied - treated as sensitive word");
    }

    @Override
    public List<String> allow() {
        return List.of("Allowed content - treated as non-sensitive word");
    }

}

Dynamic Loading

MongoPlus provides dynamic loading of sensitive word blacklists and whitelists, allowing adding and removing words at runtime.

java

// Inject SensitiveWordManager
@Resource
private SensitiveWordManager sensitiveWordManager;

// Blacklist

/** ==================Add================== */

/**
 * Add sensitive words
 * @param words Sensitive words
 */
void addWordBlacklist(String... words);

/**
 * Add sensitive words
 * @param words Sensitive words
 */
void addWordBlacklist(Collection<String> words);

/**
 * Add sensitive words from file
 * @param wordFile Path to sensitive word file
 */
void addWordBlackList(Path wordFile);

/** ==================Remove================== */

/**
 * Remove sensitive words
 * @param words Sensitive words
 */
void removeSensitiveWord(String... words);

/**
 * Remove sensitive words
 * @param words Sensitive words
 */
void removeSensitiveWord(Collection<String> words);

/**
 * Remove sensitive words from file
 * @param wordFile Path to sensitive word file
 */
void removeSensitiveWord(Path wordFile);

// Whitelist

/** ==================Add================== */

/**
 * Add words to the whitelist
 * @param words Sensitive words
 */
void addSensitiveWordAllow(String... words);

/**
 * Add words to the whitelist
 * @param words Sensitive words
 */
void addSensitiveWordAllow(Collection<String> words);

/**
 * Add words to the whitelist from file
 * @param wordFile Path to sensitive word file
 */
void addSensitiveWordAllow(Path wordFile);

/** ==================Remove================== */

/**
 * Remove words from the whitelist
 * @param words Sensitive words
 */
void removeSensitiveWordAllow(String... words);

/**
 * Remove words from the whitelist
 * @param words Sensitive words
 */
void removeSensitiveWordAllow(Collection<String> words);

/**
 * Remove words from the whitelist from file
 * @param wordFile Path to sensitive word file
 */
void removeSensitiveWordAllow(Path wordFile);