敏感词管理接口完善
parent
757bc0e5fc
commit
7ad15df234
@ -0,0 +1,16 @@
|
||||
package com.example.venue_reservation_service.config;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CacheInitializer {
|
||||
|
||||
@PostConstruct // 在Bean初始化后执行[5](@ref)
|
||||
public void init() {
|
||||
log.info("敏感词库已加载到Redis");
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.example.venue_reservation_service.controller;
|
||||
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("/word")
|
||||
@CrossOrigin
|
||||
@Api("敏感词管理")
|
||||
public class WordController {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.venue_reservation_service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WordDTO extends PageDTO{
|
||||
|
||||
private Integer level;
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.example.venue_reservation_service.filter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SensitiveFilter {
|
||||
private final TrieNode root = new TrieNode();
|
||||
private static final String REPLACEMENT = "***"; // 敏感词替换字符
|
||||
|
||||
// 构建字典树
|
||||
public void buildTrie(List<String> sensitiveWords) {
|
||||
for (String word : sensitiveWords) {
|
||||
TrieNode currentNode = root;
|
||||
// 逐个字符插入树中
|
||||
for (char c : word.toCharArray()) {
|
||||
TrieNode child = currentNode.getChild(c);
|
||||
if (child == null) {
|
||||
child = new TrieNode();
|
||||
currentNode.addChild(c, child);
|
||||
}
|
||||
currentNode = child;
|
||||
}
|
||||
currentNode.setEnd(true); // 标记敏感词结尾
|
||||
}
|
||||
}
|
||||
|
||||
// 敏感词过滤主方法
|
||||
public String filter(String text) {
|
||||
if (text == null || text.isEmpty()) return text;
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
TrieNode currentNode = root;
|
||||
int begin = 0; // 当前检测起始位置
|
||||
int position = 0; // 当前扫描位置
|
||||
|
||||
while (position < text.length()) {
|
||||
char c = text.charAt(position);
|
||||
|
||||
// 特殊字符处理(如空格、符号)
|
||||
if (isSymbol(c)) {
|
||||
if (currentNode == root) {
|
||||
result.append(c);
|
||||
begin++;
|
||||
}
|
||||
position++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 移动到下一节点
|
||||
currentNode = currentNode.getChild(c);
|
||||
|
||||
if (currentNode == null) {
|
||||
// 未匹配到敏感词
|
||||
result.append(text.charAt(begin));
|
||||
position = ++begin;
|
||||
currentNode = root; // 重置根节点
|
||||
} else if (currentNode.isEnd()) {
|
||||
// 发现完整敏感词 → 替换
|
||||
result.append(REPLACEMENT);
|
||||
begin = ++position;
|
||||
currentNode = root;
|
||||
} else {
|
||||
// 部分匹配,继续扫描
|
||||
position++;
|
||||
}
|
||||
}
|
||||
result.append(text.substring(begin)); // 添加剩余文本
|
||||
return result.toString();
|
||||
}
|
||||
// 特殊字符检测(可根据需求扩展)
|
||||
private boolean isSymbol(char c) {
|
||||
// 过滤非字母数字字符(可自定义规则)
|
||||
return !Character.isLetterOrDigit(c) && ((int) c < 0x2E80 || (int) c > 0x9FFF);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.example.venue_reservation_service.filter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TrieNode {
|
||||
// 存储子节点(字符 → 节点映射)
|
||||
private final Map<Character, TrieNode> children = new HashMap<>();
|
||||
// 标记是否为敏感词终点
|
||||
private boolean isEnd = false;
|
||||
|
||||
// 添加子节点
|
||||
public void addChild(char c, TrieNode node) {
|
||||
children.put(c, node);
|
||||
}
|
||||
|
||||
// 获取子节点
|
||||
public TrieNode getChild(char c) {
|
||||
return children.get(c);
|
||||
}
|
||||
|
||||
// 是否为敏感词结尾
|
||||
public boolean isEnd() {
|
||||
return isEnd;
|
||||
}
|
||||
|
||||
// 设置终点标记
|
||||
public void setEnd(boolean end) {
|
||||
isEnd = end;
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package com.example.venue_reservation_service.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.venue_reservation_service.domain.SensitiveWord;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SensitiveWordMapper extends BaseMapper<SensitiveWord> {
|
||||
}
|
||||
package com.example.venue_reservation_service.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.venue_reservation_service.domain.SensitiveWord;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SensitiveWordMapper extends BaseMapper<SensitiveWord> {
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package com.example.venue_reservation_service.mapper;
|
||||
|
||||
import com.example.venue_reservation_service.domain.Word;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author 31586
|
||||
* @description 针对表【venue_word(敏感词库表)】的数据库操作Mapper
|
||||
* @createDate 2025-06-03 15:00:23
|
||||
* @Entity generator.domain.Word
|
||||
*/
|
||||
public interface WordMapper extends BaseMapper<Word> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,23 +1,24 @@
|
||||
package com.example.venue_reservation_service.service;
|
||||
|
||||
import com.example.venue_reservation_service.domain.SensitiveWord;
|
||||
import com.example.venue_reservation_service.dto.PageDTO;
|
||||
import com.example.venue_reservation_service.dto.SensitiveWordDTO;
|
||||
import com.example.venue_reservation_service.dto.WordDTO;
|
||||
import com.example.venue_reservation_service.vo.Result;
|
||||
|
||||
public interface SensitiveWordService {
|
||||
|
||||
// 获取敏感词数据
|
||||
Result getSensitiveWords(PageDTO dto);
|
||||
|
||||
Result getSensitiveWords(WordDTO dto);
|
||||
|
||||
// 添加敏感词
|
||||
Result addSensitiveWord(SensitiveWordDTO dto);
|
||||
|
||||
|
||||
// 更新敏感词
|
||||
|
||||
Result updateSensitiveWord(Integer id, SensitiveWordDTO dto);
|
||||
Result updateSensitiveWord(SensitiveWord word);
|
||||
|
||||
// 移除敏感词
|
||||
Result removeSensitiveWord(Integer id);
|
||||
|
||||
Result set(Integer id);
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
package com.example.venue_reservation_service.service;
|
||||
|
||||
import com.example.venue_reservation_service.domain.Word;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author 31586
|
||||
* @description 针对表【venue_word(敏感词库表)】的数据库操作Service
|
||||
* @createDate 2025-06-03 15:00:23
|
||||
*/
|
||||
public interface WordService extends IService<Word> {
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package com.example.venue_reservation_service.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.example.venue_reservation_service.domain.Word;
|
||||
import com.example.venue_reservation_service.service.WordService;
|
||||
import com.example.venue_reservation_service.mapper.WordMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author 31586
|
||||
* @description 针对表【venue_word(敏感词库表)】的数据库操作Service实现
|
||||
* @createDate 2025-06-03 15:00:23
|
||||
*/
|
||||
@Service
|
||||
public class WordServiceImpl extends ServiceImpl<WordMapper, Word>
|
||||
implements WordService{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue