feat(note): 增加私密笔记功能
- 在 MarkdownFile 表中添加 is_private 字段,用于标记笔记是否私密- 修改 MarkdownController 中的 getMarkdownContent 方法,增加对私密笔记的处理逻辑 - 更新 MarkdownFileService 接口和 MarkdownFileServiceImpl 实现类,支持根据认证状态获取笔记内容 - 优化未认证用户访问私密笔记时的处理,只显示标题不显示内容
This commit is contained in:
@@ -10,6 +10,9 @@ import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
@@ -36,8 +39,15 @@ public class MarkdownController {
|
||||
})
|
||||
@GetMapping("/{id}")
|
||||
public R<String> getMarkdownContent(@PathVariable Long id) {
|
||||
MarkdownFile file = markdownFileService.getMarkdownById(id);
|
||||
// 获取当前认证状态
|
||||
boolean isAuthenticated = isUserAuthenticated();
|
||||
|
||||
MarkdownFile file = markdownFileService.getMarkdownById(id, isAuthenticated);
|
||||
if (ObjectUtil.isNotNull(file)) {
|
||||
// 如果是私密笔记且用户未认证,只返回标题
|
||||
if (file.getIsPrivate() != null && file.getIsPrivate() == 1 && !isAuthenticated) {
|
||||
return R.success(""); // 返回空内容,只显示标题
|
||||
}
|
||||
return R.success(file.getContent());
|
||||
}
|
||||
return R.fail();
|
||||
@@ -102,4 +112,20 @@ public class MarkdownController {
|
||||
List<MarkdownFileVO> files = markdownFileService.getRecentFiles(12);
|
||||
return R.success(files);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否已认证
|
||||
* @return 是否已认证
|
||||
*/
|
||||
private boolean isUserAuthenticated() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null || !authentication.isAuthenticated()) {
|
||||
return false;
|
||||
}
|
||||
Object principal = authentication.getPrincipal();
|
||||
if (principal instanceof UserDetails) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user