系统架构设计
采用SSM(Spring+SpringMVC+MyBatis)作为后端框架,Vue.js作为前端技术栈。系统分为三个模块:用户管理、电子书内容管理、敏感字识别与处理模块。通过RESTful API实现前后端数据交互。
后端使用Spring Security实现权限控制,MyBatis-Plus简化数据库操作。前端采用Vue CLI搭建工程,使用Element UI组件库。敏感字识别算法基于AC自动机实现高效匹配。
数据库设计
核心表包括用户表(user)、电子书表(ebook)、敏感字库表(sensitive_words)、识别记录表(detect_record)。
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(100) NOT NULL,
`role` enum('admin','user') NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `sensitive_words` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`word` varchar(100) NOT NULL,
`level` enum('high','medium','low') NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `ebook` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`content` longtext NOT NULL,
`status` enum('pending','approved','rejected') NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
);
CREATE TABLE `detect_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ebook_id` int(11) NOT NULL,
`detect_time` datetime NOT NULL,
`sensitive_count` int(11) NOT NULL,
`details` json DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`ebook_id`) REFERENCES `ebook` (`id`)
);
核心功能实现
敏感字识别服务:
@Service
public class SensitiveWordDetector {
private final TrieNode root = new TrieNode();
@PostConstruct
public void init() {
List<SensitiveWord> words = sensitiveWordMapper.selectList(null);
words.forEach(word -> addWord(word.getWord()));
}
private void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
node = node.getChildren().computeIfAbsent(c, k -> new TrieNode());
}
node.setEnd(true);
}
public DetectionResult detect(String text) {
List<MatchResult> matches = new ArrayList<>();
for (int i = 0; i < text.length(); i++) {
TrieNode node = root;
for (int j = i; j < text.length(); j++) {
node = node.getChildren().get(text.charAt(j));
if (node == null) break;
if (node.isEnd()) {
matches.add(new MatchResult(i, j, text.substring(i, j+1)));
}
}
}
return new DetectionResult(matches);
}
}
Vue前端关键组件:
<template>
<el-upload
action="/api/ebook/upload"
:on-success="handleSuccess"
:before-upload="checkFile">
<el-button type="primary">上传电子书</el-button>
</el-upload>
<el-table :data="detectResults">
<el-table-column prop="word" label="敏感词"/>
<el-table-column prop="positions" label="位置"/>
<el-table-column prop="level" label="风险等级"/>
</el-table>
</template>
<script>
export default {
methods: {
handleSuccess(response) {
this.detectResults = response.data.matches;
},
checkFile(file) {
return file.type === 'text/plain' || file.type === 'application/pdf';
}
}
}
</script>
系统测试设计
单元测试示例:
@SpringBootTest
public class SensitiveWordDetectorTest {
@Autowired
private SensitiveWordDetector detector;
@Test
public void testDetection() {
String text = "测试敏感词过滤效果";
DetectionResult result = detector.detect(text);
assertEquals(1, result.getMatches().size());
assertEquals("敏感词", result.getMatches().get(0).getWord());
}
}
接口测试用例:
- 上传电子书接口:验证文件类型检查、内容解析、敏感词识别
- 敏感词管理接口:测试增删改查和批量导入功能
- 审核接口:验证状态变更和权限控制
部署方案
- 后端打包为JAR文件,使用Spring Boot内嵌Tomcat
- 前端使用npm run build生成静态资源
- Nginx配置反向代理和静态资源服务
- MySQL数据库配置主从复制保证数据安全
系统支持Docker容器化部署,提供docker-compose.yml文件实现一键部署。监控模块集成Prometheus和Grafana实现性能监控。