企业知识库系统 Java+MySQL实现文档管理与权限控制

技术选型与架构设计
Java作为后端开发语言,结合Spring Boot框架快速搭建系统。MySQL作为关系型数据库存储结构化数据,文档实体采用BLOB类型存储或结合文件系统路径存储。权限控制采用RBAC(基于角色的访问控制)模型,通过Spring Security实现认证授权。
数据库表结构设计
用户表(user)
字段包含user_id(主键)、username、password(加密存储)、department_id等。密码需使用BCryptPasswordEncoder加密,避免明文存储。
CREATE TABLE user (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
department_id INT,
is_active BOOLEAN DEFAULT TRUE
);
文档表(document)
包含doc_id(主键)、title、file_path(物理存储路径)、file_size、upload_time、uploader_id(外键)等字段。文件实际存储可采用MinIO或本地文件系统。
CREATE TABLE document (
doc_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
file_path VARCHAR(512) NOT NULL,
file_size BIGINT,
upload_time DATETIME DEFAULT CURRENT_TIMESTAMP,
uploader_id INT,
FOREIGN KEY (uploader_id) REFERENCES user(user_id)
);
权限控制实现
角色权限关联表
设计role、permission、role_permission三张表实现RBAC模型。权限颗粒度可细化到文档的CRUD操作。
CREATE TABLE role (
role_id INT PRIMARY KEY AUTO_INCREMENT,
role_name VARCHAR(50) UNIQUE NOT NULL
);
CREATE TABLE permission (
perm_id INT PRIMARY KEY AUTO_INCREMENT,
perm_name VARCHAR(50) UNIQUE NOT NULL,
resource_type VARCHAR(50) NOT NULL
);
CREATE TABLE role_permission (
role_id INT NOT NULL,
perm_id INT NOT NULL,
PRIMARY KEY (role_id, perm_id),
FOREIGN KEY (role_id) REFERENCES role(role_id),
FOREIGN KEY (perm_id) REFERENCES permission(perm_id)
);
Spring Security配置
通过自定义UserDetailsService和JWT实现认证,@PreAuthorize注解实现方法级权限控制。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/auth/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.addFilter(new JwtAuthorizationFilter(authenticationManager()));
}
}
文档管理核心功能
文件上传接口
采用MultipartFile接收文件,校验文件类型和大小后存储到指定路径。
@PostMapping("/upload")
@PreAuthorize("hasPermission('document', 'create')")
public ResponseEntity<String> uploadDocument(
@RequestParam("file") MultipipartFile file,
@RequestParam String title) {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body("文件为空");
}
String filePath = storageService.store(file);
Document doc = new Document(title, filePath, file.getSize());
documentRepository.save(doc);
return ResponseEntity.ok("上传成功");
}
文件下载权限校验
在下载接口中校验用户是否具有当前文档的读取权限。
@GetMapping("/download/{docId}")
@PreAuthorize("hasPermission(#docId, 'document', 'read')")
public ResponseEntity<Resource> downloadDocument(@PathVariable Long docId) {
Document doc = documentService.getById(docId);
Resource resource = storageService.loadAsResource(doc.getFilePath());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + doc.getTitle() + "\"")
.body(resource);
}
高级功能扩展
文档版本控制
通过新增document_version表实现,每次更新时保留历史版本。表结构包含version_id、doc_id、file_path、version_number等字段。
全文检索支持
集成Elasticsearch建立文档索引,实现标题和内容的快速检索。需定期同步MySQL数据到ES索引。
操作日志审计
建立operation_log表记录用户操作,包含user_id、operation_type、target_id、operation_time等字段。通过AOP切面统一采集日志。
更多推荐


所有评论(0)