longz
2026-06-02 937f99a139bedbacdf15e25cfd55aa592f56082c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.sinata.appointment.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinata.appointment.constants.CacheNames;
import com.sinata.appointment.entity.BizSensitiveWord;
import com.sinata.appointment.mapper.BizSensitiveWordMapper;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
@Service
@RequiredArgsConstructor
public class SensitiveWordFilterService {
 
    private final BizSensitiveWordMapper bizSensitiveWordMapper;
    private final CacheManager cacheManager;
 
    public String filter(String text) {
        if (!StringUtils.hasText(text)) {
            return text;
        }
        List<String> words = enabledWords();
        if (words.isEmpty()) {
            return text;
        }
 
        String result = text;
        for (String word : words) {
            result = replaceIgnoreCase(result, word);
        }
        return result;
    }
 
    public void evictCache() {
        Cache cache = cacheManager.getCache(CacheNames.BIZ_SENSITIVE_WORD);
        if (cache != null) {
            cache.evict(CacheNames.BIZ_SENSITIVE_WORD_ENABLED_KEY);
        }
    }
 
    @SuppressWarnings("unchecked")
    public List<String> enabledWords() {
        Cache cache = cacheManager.getCache(CacheNames.BIZ_SENSITIVE_WORD);
        if (cache != null) {
            Cache.ValueWrapper cached = cache.get(CacheNames.BIZ_SENSITIVE_WORD_ENABLED_KEY);
            if (cached != null && cached.get() instanceof List<?> list) {
                return (List<String>) list;
            }
        }
 
        List<String> words = bizSensitiveWordMapper
                .selectList(
                        new LambdaQueryWrapper<BizSensitiveWord>()
                                .eq(BizSensitiveWord::getDelFlag, 0)
                                .eq(BizSensitiveWord::getEnabled, 1))
                .stream()
                .map(BizSensitiveWord::getWord)
                .filter(StringUtils::hasText)
                .map(String::trim)
                .distinct()
                .sorted(Comparator.comparingInt(String::length).reversed())
                .toList();
        if (cache != null) {
            cache.put(CacheNames.BIZ_SENSITIVE_WORD_ENABLED_KEY, words);
        }
        return words;
    }
 
    private String replaceIgnoreCase(String source, String word) {
        if (!StringUtils.hasText(source) || !StringUtils.hasText(word)) {
            return source;
        }
 
        String lowerSource = source.toLowerCase(Locale.ROOT);
        String lowerWord = word.toLowerCase(Locale.ROOT);
        StringBuilder builder = new StringBuilder(source.length());
        int cursor = 0;
        int matchIndex = lowerSource.indexOf(lowerWord, cursor);
        while (matchIndex >= 0) {
            builder.append(source, cursor, matchIndex);
            builder.append("*".repeat(word.length()));
            cursor = matchIndex + word.length();
            matchIndex = lowerSource.indexOf(lowerWord, cursor);
        }
        builder.append(source, cursor, source.length());
        return builder.toString();
    }
}