From 7deec931ec4a13059a8fc91ac5f7b7520b8104a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A8=80=E6=9B=8C?= <847064370@qq.com> Date: Fri, 18 Dec 2020 16:39:16 +0800 Subject: [PATCH 01/19] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b62aa0a2..9561d0fd 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ # 关于项目 该博客是基于SSM实现的个人博客系统,适合初学SSM和个人博客制作的同学学习。
不支持用户注册,需要多用户的可以去看 [SENS](https://github.com/saysky/SENS) 和 [ChuyunBlog](https://github.com/saysky/ChuyunBlog)
-主要涉及技术包括的包括 Maven、Spring、SpringMVC、MyBatis、Redis、JSP、MySQL等。
+主要涉及技术包括的包括 Maven、Spring、SpringMVC、MyBatis、JSP、MySQL等。
详细介绍:[https://liuyanzhao.com/6347.html](https://liuyanzhao.com/6347.html)
预览地址 [https://forestblog.liuyanzhao.com](https://forestblog.liuyanzhao.com) From 5679048530a368260232e4ec1406994f8f3750ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A8=80=E6=9B=8C?= <847064370@qq.com> Date: Wed, 27 Jan 2021 14:49:08 +0800 Subject: [PATCH 02/19] Update Category.java --- .../main/java/com/liuyanzhao/ssm/blog/entity/Category.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Category.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Category.java index 6244d429..904f7676 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Category.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Category.java @@ -48,6 +48,8 @@ public Category(Integer categoryId, String categoryName) { public Category(Integer categoryId) { this.categoryId = categoryId; } + + public Category() {} /** * 未分类 @@ -59,4 +61,4 @@ public static Category Default() { } -} \ No newline at end of file +} From 7656ce2dbe9931eb024dce7c89be990e631fd136 Mon Sep 17 00:00:00 2001 From: saysky <847064370@qq.com> Date: Thu, 25 Feb 2021 18:28:43 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E7=AD=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 14 +- ForestBlog/pom.xml | 17 + .../controller/admin/AdminController.java | 120 +++++- .../admin/BackArticleController.java | 110 +++++- .../admin/BackCommentController.java | 94 ++++- .../controller/admin/BackUserController.java | 16 - .../admin/UploadFileController.java | 3 +- .../controller/home/CommentController.java | 31 +- .../blog/controller/home/IndexController.java | 11 +- .../liuyanzhao/ssm/blog/entity/Comment.java | 9 +- .../com/liuyanzhao/ssm/blog/entity/User.java | 5 + .../com/liuyanzhao/ssm/blog/enums/Role.java | 4 +- .../liuyanzhao/ssm/blog/enums/UserRole.java | 40 ++ .../blog/interceptor/AdminInterceptor.java | 45 +++ .../blog/interceptor/LoginInterceptor.java | 39 ++ .../blog/interceptor/SecurityInterceptor.java | 1 + .../ssm/blog/mapper/ArticleMapper.java | 17 +- .../ssm/blog/mapper/CommentMapper.java | 33 +- .../ssm/blog/service/ArticleService.java | 2 +- .../ssm/blog/service/CommentService.java | 15 +- .../blog/service/impl/ArticleServiceImpl.java | 21 +- .../blog/service/impl/CommentServiceImpl.java | 28 +- .../blog/service/impl/UserServiceImpl.java | 19 + ForestBlog/src/main/resources/db.properties | 2 +- .../main/resources/mapper/ArticleMapper.xml | 20 +- .../main/resources/mapper/CommentMapper.xml | 48 ++- .../src/main/resources/mapper/UserMapper.xml | 8 +- .../src/main/resources/spring/spring-mvc.xml | 74 +++- .../WEB-INF/view/Admin/Article/index.jsp | 5 + .../WEB-INF/view/Admin/Comment/index.jsp | 10 +- .../WEB-INF/view/Admin/Comment/reply.jsp | 19 - .../WEB-INF/view/Admin/Public/framework.jsp | 35 +- .../WEB-INF/view/Admin/User/editProfile.jsp | 147 ++++++++ .../WEB-INF/view/Admin/User/profile.jsp | 5 +- .../main/webapp/WEB-INF/view/Admin/login.jsp | 3 +- .../webapp/WEB-INF/view/Admin/register.jsp | 165 +++++++++ .../webapp/WEB-INF/view/Home/Error/403.jsp | 62 ++++ .../WEB-INF/view/Home/Page/articleDetail.jsp | 51 +-- .../WEB-INF/view/Home/Public/part/header.jsp | 4 +- .../main/webapp/resource/assets/css/style.css | 2 +- .../resource/assets/img/avatar/avatar.png | Bin 0 -> 538302 bytes .../resource/assets/img/avatar/avatar1.jpg | Bin 0 -> 27063 bytes .../resource/assets/img/avatar/avatar2.jpeg | Bin 0 -> 46932 bytes .../resource/assets/img/avatar/avatar3.jpeg | Bin 0 -> 16346 bytes .../ssm/blog/mapper/ArticleMapperTest.java | 31 ++ .../ssm/blog/service/ArticleServiceTest.java | 2 +- .../ssm/blog/service/UserServiceTest.java | 2 +- forest_blog.sql | 348 +++++++++++++++--- 48 files changed, 1498 insertions(+), 239 deletions(-) mode change 100644 => 100755 .gitignore create mode 100644 ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/UserRole.java create mode 100755 ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/AdminInterceptor.java create mode 100755 ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/LoginInterceptor.java create mode 100755 ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/editProfile.jsp create mode 100755 ForestBlog/src/main/webapp/WEB-INF/view/Admin/register.jsp create mode 100755 ForestBlog/src/main/webapp/WEB-INF/view/Home/Error/403.jsp create mode 100644 ForestBlog/src/main/webapp/resource/assets/img/avatar/avatar.png create mode 100755 ForestBlog/src/main/webapp/resource/assets/img/avatar/avatar1.jpg create mode 100755 ForestBlog/src/main/webapp/resource/assets/img/avatar/avatar2.jpeg create mode 100755 ForestBlog/src/main/webapp/resource/assets/img/avatar/avatar3.jpeg create mode 100644 ForestBlog/src/test/java/com/liuyanzhao/ssm/blog/mapper/ArticleMapperTest.java diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 817acb6e..032ce5c8 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ !.mvn/wrapper/maven-wrapper.jar -target/ + ### STS ### .apt_generated .classpath @@ -13,6 +13,8 @@ target/ *.iws *.iml *.ipr +*.log +*.gz ### NetBeans ### nbproject/private/ @@ -22,5 +24,11 @@ dist/ nbdist/ .nb-gradle/ -#Mac -.DS_Store \ No newline at end of file +.DS_Store + +target/ +.mvn/ +mvnw.cmd +mvnw +sens-log/ +/templates \ No newline at end of file diff --git a/ForestBlog/pom.xml b/ForestBlog/pom.xml index 6cf869cc..2aaf34f9 100755 --- a/ForestBlog/pom.xml +++ b/ForestBlog/pom.xml @@ -14,6 +14,7 @@ 1.8 UTF-8 4.3.19.RELEASE + 2.10.1 @@ -224,6 +225,22 @@ 4.1.13 + + com.fasterxml.jackson.core + jackson-databind + ${jackson-databind-version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson-databind-version} + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson-databind-version} + + diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/AdminController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/AdminController.java index 888403ef..9b4450ee 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/AdminController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/AdminController.java @@ -1,8 +1,10 @@ package com.liuyanzhao.ssm.blog.controller.admin; +import com.liuyanzhao.ssm.blog.dto.JsonResult; import com.liuyanzhao.ssm.blog.entity.Article; import com.liuyanzhao.ssm.blog.entity.Comment; import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.UserRole; import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.service.CommentService; import com.liuyanzhao.ssm.blog.service.UserService; @@ -10,9 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -45,12 +49,19 @@ public class AdminController { * @return */ @RequestMapping("/admin") - public String index(Model model) { + public String index(Model model, HttpSession session) { + User user = (User) session.getAttribute("user"); + Integer userId = null; + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + userId = user.getUserId(); + } //文章列表 - List
articleList = articleService.listRecentArticle(5); + List
articleList = articleService.listRecentArticle(userId, 5); model.addAttribute("articleList", articleList); + //评论列表 - List commentList = commentService.listRecentComment(5); + List commentList = commentService.listRecentComment(userId, 5); model.addAttribute("commentList", commentList); return "Admin/index"; } @@ -65,6 +76,17 @@ public String loginPage() { return "Admin/login"; } + + /** + * 登录页面显示 + * + * @return + */ + @RequestMapping("/register") + public String registerPage() { + return "Admin/register"; + } + /** * 登录验证 * @@ -113,6 +135,47 @@ public String loginVerify(HttpServletRequest request, HttpServletResponse respon return result; } + /** + * 登录验证 + * + * @param request + * @return + */ + @RequestMapping(value = "/registerSubmit", method = RequestMethod.POST) + @ResponseBody + public JsonResult registerSubmit(HttpServletRequest request) { + String username = request.getParameter("username"); + String nickname = request.getParameter("nickname"); + String email = request.getParameter("email"); + String password = request.getParameter("password"); + User checkUserName = userService.getUserByName(username); + if (checkUserName != null) { + return new JsonResult().fail("用户名已存在"); + } + User checkEmail = userService.getUserByEmail(username); + if (checkEmail != null) { + return new JsonResult().fail("电子邮箱已存在"); + } + + // 添加用户 + User user = new User(); + user.setUserAvatar("/img/avatar/avatar.png"); + user.setUserName(username); + user.setUserNickname(nickname); + user.setUserPass(password); + user.setUserEmail(email); + user.setUserStatus(1); + user.setArticleCount(0); + user.setUserRole(UserRole.USER.getValue()); + try { + userService.insertUser(user); + } catch (Exception e) { + e.printStackTrace(); + return new JsonResult().fail("系统异常"); + } + return new JsonResult().ok("注册成功"); + } + /** * 退出登录 * @@ -127,4 +190,55 @@ public String logout(HttpSession session) { } + /** + * 基本信息页面显示 + * + * @return + */ + @RequestMapping(value = "/admin/profile") + public ModelAndView userProfileView(HttpSession session) { + + ModelAndView modelAndView = new ModelAndView(); + User sessionUser = (User) session.getAttribute("user"); + User user = userService.getUserById(sessionUser.getUserId()); + modelAndView.addObject("user", user); + + modelAndView.setViewName("Admin/User/profile"); + return modelAndView; + } + + /** + * 编辑个人信息页面显示 + * + * @param loginUser + * @return + */ + @RequestMapping(value = "/admin/profile/edit") + public ModelAndView editUserView(HttpSession session) { + ModelAndView modelAndView = new ModelAndView(); + User loginUser = (User) session.getAttribute("user"); + User user = userService.getUserById(loginUser.getUserId()); + modelAndView.addObject("user", user); + + modelAndView.setViewName("Admin/User/editProfile"); + return modelAndView; + } + + + /** + * 编辑用户提交 + * + * @param user + * @return + */ + @RequestMapping(value = "/admin/profile/save", method = RequestMethod.POST) + public String saveProfile(User user, HttpSession session) { + User dbUser = (User) session.getAttribute("user"); + + user.setUserId(dbUser.getUserId()); + userService.updateUser(user); + return "redirect:/admin/profile"; + } + + } diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackArticleController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackArticleController.java index 86941d64..459e1a45 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackArticleController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackArticleController.java @@ -3,7 +3,9 @@ import cn.hutool.http.HtmlUtil; import com.github.pagehelper.PageInfo; import com.liuyanzhao.ssm.blog.dto.ArticleParam; +import com.liuyanzhao.ssm.blog.dto.JsonResult; import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.enums.UserRole; import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.service.CategoryService; import com.liuyanzhao.ssm.blog.service.TagService; @@ -14,16 +16,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Objects; /** @@ -49,7 +49,8 @@ public class BackArticleController { @RequestMapping(value = "") public String index(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, @RequestParam(required = false, defaultValue = "10") Integer pageSize, - @RequestParam(required = false) String status, Model model) { + @RequestParam(required = false) String status, Model model, + HttpSession session) { HashMap criteria = new HashMap<>(1); if (status == null) { model.addAttribute("pageUrlPrefix", "/admin/article?pageIndex"); @@ -57,6 +58,12 @@ public String index(@RequestParam(required = false, defaultValue = "1") Integer criteria.put("status", status); model.addAttribute("pageUrlPrefix", "/admin/article?status=" + status + "&pageIndex"); } + + User user = (User) session.getAttribute("user"); + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + criteria.put("userId", user.getUserId()); + } PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); model.addAttribute("pageInfo", articlePageInfo); return "Admin/Article/index"; @@ -132,8 +139,17 @@ public String insertArticleSubmit(HttpSession session, ArticleParam articleParam * * @param id 文章ID */ - @RequestMapping(value = "/delete/{id}") - public void deleteArticle(@PathVariable("id") Integer id) { + @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) + public void deleteArticle(@PathVariable("id") Integer id, HttpSession session) { + Article dbArticle = articleService.getArticleByStatusAndId(null, id); + if (dbArticle == null) { + return; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(dbArticle.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return; + } articleService.deleteArticle(id); } @@ -145,22 +161,27 @@ public void deleteArticle(@PathVariable("id") Integer id) { * @return */ @RequestMapping(value = "/edit/{id}") - public ModelAndView editArticleView(@PathVariable("id") Integer id) { - ModelAndView modelAndView = new ModelAndView(); + public String editArticleView(@PathVariable("id") Integer id, Model model, HttpSession session) { Article article = articleService.getArticleByStatusAndId(null, id); - modelAndView.addObject("article", article); + if (article == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(article.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } + model.addAttribute("article", article); List categoryList = categoryService.listCategory(); - modelAndView.addObject("categoryList", categoryList); + model.addAttribute("categoryList", categoryList); List tagList = tagService.listTag(); - modelAndView.addObject("tagList", tagList); - + model.addAttribute("tagList", tagList); - modelAndView.setViewName("Admin/Article/edit"); - return modelAndView; + return "Admin/Article/edit"; } @@ -171,7 +192,16 @@ public ModelAndView editArticleView(@PathVariable("id") Integer id) { * @return */ @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) - public String editArticleSubmit(ArticleParam articleParam) { + public String editArticleSubmit(ArticleParam articleParam, HttpSession session) { + Article dbArticle = articleService.getArticleByStatusAndId(null, articleParam.getArticleId()); + if (dbArticle == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(dbArticle.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } Article article = new Article(); article.setArticleId(articleParam.getArticleId()); article.setArticleTitle(articleParam.getArticleTitle()); @@ -208,6 +238,54 @@ public String editArticleSubmit(ArticleParam articleParam) { return "redirect:/admin/article"; } + /** + * 后台添加文章提交操作 + * + * @param articleParam + * @return + */ + @RequestMapping(value = "/insertDraftSubmit", method = RequestMethod.POST) + public String insertDraftSubmit(HttpSession session, ArticleParam articleParam) { + Article article = new Article(); + //用户ID + User user = (User) session.getAttribute("user"); + if (user != null) { + article.setArticleUserId(user.getUserId()); + } + article.setArticleTitle(articleParam.getArticleTitle()); + //文章摘要 + int summaryLength = 150; + String summaryText = HtmlUtil.cleanHtmlTag(articleParam.getArticleContent()); + if (summaryText.length() > summaryLength) { + String summary = summaryText.substring(0, summaryLength); + article.setArticleSummary(summary); + } else { + article.setArticleSummary(summaryText); + } + article.setArticleContent(articleParam.getArticleContent()); + article.setArticleStatus(articleParam.getArticleStatus()); + //填充分类 + List categoryList = new ArrayList<>(); + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleParentCategoryId())); + } + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleChildCategoryId())); + } + article.setCategoryList(categoryList); + //填充标签 + List tagList = new ArrayList<>(); + if (articleParam.getArticleTagIds() != null) { + for (int i = 0; i < articleParam.getArticleTagIds().size(); i++) { + Tag tag = new Tag(articleParam.getArticleTagIds().get(i)); + tagList.add(tag); + } + } + article.setTagList(tagList); + + articleService.insertArticle(article); + return "redirect:/admin"; + } } diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackCommentController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackCommentController.java index 4b9b950b..ebf5e59a 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackCommentController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackCommentController.java @@ -1,10 +1,15 @@ package com.liuyanzhao.ssm.blog.controller.admin; +import cn.hutool.http.HtmlUtil; import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.dto.JsonResult; import com.liuyanzhao.ssm.blog.entity.Article; import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.User; import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.enums.Role; +import com.liuyanzhao.ssm.blog.enums.UserRole; import com.liuyanzhao.ssm.blog.util.MyUtils; import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.service.CommentService; @@ -14,8 +19,8 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; -import java.util.Date; -import java.util.List; +import javax.servlet.http.HttpSession; +import java.util.*; /** @@ -33,18 +38,47 @@ public class BackCommentController { /** * 评论页面 + * 我发送的评论 * * @param pageIndex 页码 * @param pageSize 页大小 * @return modelAndView */ @RequestMapping(value = "") - public String commentListView(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, - @RequestParam(required = false, defaultValue = "10") Integer pageSize, - Model model) { - PageInfo commentPageInfo = commentService.listCommentByPage(pageIndex, pageSize); + public String commentList(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + HttpSession session, + Model model) { + User user = (User) session.getAttribute("user"); + HashMap criteria = new HashMap<>(); + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + criteria.put("userId", user.getUserId()); + } + PageInfo commentPageInfo = commentService.listCommentByPage(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", commentPageInfo); + model.addAttribute("pageUrlPrefix", "/admin/comment?pageIndex"); + return "Admin/Comment/index"; + } + + + /** + * 评论页面 + * 我收到的评论 + * + * @param pageIndex 页码 + * @param pageSize 页大小 + * @return modelAndView + */ + @RequestMapping(value = "/receive") + public String myReceiveComment(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + HttpSession session, + Model model) { + User user = (User) session.getAttribute("user"); + PageInfo commentPageInfo = commentService.listReceiveCommentByPage(pageIndex, pageSize, user.getUserId()); model.addAttribute("pageInfo", commentPageInfo); - model.addAttribute("pageUrlPrefix","/admin/comment?pageIndex"); + model.addAttribute("pageUrlPrefix", "/admin/comment?pageIndex"); return "Admin/Comment/index"; } @@ -57,13 +91,19 @@ public String commentListView(@RequestParam(required = false, defaultValue = "1" */ @RequestMapping(value = "/insert", method = {RequestMethod.POST}, produces = {"text/plain;charset=UTF-8"}) @ResponseBody - public void insertComment(HttpServletRequest request, Comment comment) { + public void insertComment(HttpServletRequest request, Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId()); + if (article == null) { + return; + } + //添加评论 + comment.setCommentUserId(user.getUserId()); comment.setCommentIp(MyUtils.getIpAddr(request)); comment.setCommentCreateTime(new Date()); commentService.insertComment(comment); //更新文章的评论数 - Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId()); articleService.updateCommentCount(article.getArticleId()); } @@ -73,8 +113,13 @@ public void insertComment(HttpServletRequest request, Comment comment) { * @param id 批量ID */ @RequestMapping(value = "/delete/{id}") - public void deleteComment(@PathVariable("id") Integer id) { + public void deleteComment(@PathVariable("id") Integer id, HttpSession session) { Comment comment = commentService.getCommentById(id); + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,没有权限 + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue()) && !Objects.equals(comment.getCommentUserId(), user.getUserId())) { + return; + } //删除评论 commentService.deleteComment(id); //删除其子评论 @@ -94,7 +139,12 @@ public void deleteComment(@PathVariable("id") Integer id) { * @return */ @RequestMapping(value = "/edit/{id}") - public String editCommentView(@PathVariable("id") Integer id, Model model) { + public String editCommentView(@PathVariable("id") Integer id, Model model, HttpSession session) { + // 没有权限操作,只有管理员可以操作 + User user = (User) session.getAttribute("user"); + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } Comment comment = commentService.getCommentById(id); model.addAttribute("comment", comment); return "Admin/Comment/edit"; @@ -108,7 +158,12 @@ public String editCommentView(@PathVariable("id") Integer id, Model model) { * @return */ @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) - public String editCommentSubmit(Comment comment) { + public String editCommentSubmit(Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + // 没有权限操作,只有管理员可以操作 + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } commentService.updateComment(comment); return "redirect:/admin/comment"; } @@ -135,14 +190,27 @@ public String replyCommentView(@PathVariable("id") Integer id, Model model) { * @return */ @RequestMapping(value = "/replySubmit", method = RequestMethod.POST) - public String replyCommentSubmit(HttpServletRequest request, Comment comment) { + public String replyCommentSubmit(HttpServletRequest request, Comment comment, HttpSession session) { //文章评论数+1 Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId()); + if (article == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent())); + comment.setCommentAuthorName(user.getUserNickname()); + comment.setCommentAuthorEmail(user.getUserEmail()); + comment.setCommentAuthorUrl(user.getUserUrl()); article.setArticleCommentCount(article.getArticleCommentCount() + 1); articleService.updateArticle(article); //添加评论 comment.setCommentCreateTime(new Date()); comment.setCommentIp(MyUtils.getIpAddr(request)); + if (Objects.equals(user.getUserId(), article.getArticleUserId())) { + comment.setCommentRole(Role.OWNER.getValue()); + } else { + comment.setCommentRole(Role.VISITOR.getValue()); + } commentService.insertComment(comment); return "redirect:/admin/comment"; } diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackUserController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackUserController.java index c1b2920e..d4dd902e 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackUserController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/BackUserController.java @@ -171,20 +171,4 @@ public String editUserSubmit(User user) { return "redirect:/admin/user"; } - /** - * 基本信息页面显示 - * - * @return - */ - @RequestMapping(value = "/profile") - public ModelAndView userProfileView(HttpSession session) { - - ModelAndView modelAndView = new ModelAndView(); - User sessionUser = (User) session.getAttribute("user"); - User user = userService.getUserById(sessionUser.getUserId()); - modelAndView.addObject("user",user); - - modelAndView.setViewName("Admin/User/profile"); - return modelAndView; - } } diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/UploadFileController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/UploadFileController.java index 28d046fa..7431f47f 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/UploadFileController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/admin/UploadFileController.java @@ -22,7 +22,8 @@ public class UploadFileController { /** * 文件保存目录,物理路径 */ - public final String rootPath = "/Users/liuyanzhao/Documents/uploads"; +// public final String rootPath = "/Users/liuyanzhao/Documents/uploads"; + public final String rootPath = "D:\\uploads"; public final String allowSuffix = ".bmp.jpg.jpeg.png.gif.pdf.doc.zip.rar.gz"; diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/CommentController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/CommentController.java index 53cd0b4f..c6ce0632 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/CommentController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/CommentController.java @@ -4,6 +4,7 @@ import com.liuyanzhao.ssm.blog.dto.JsonResult; import com.liuyanzhao.ssm.blog.entity.Article; import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.User; import com.liuyanzhao.ssm.blog.enums.ArticleStatus; import com.liuyanzhao.ssm.blog.enums.Role; import com.liuyanzhao.ssm.blog.service.ArticleService; @@ -13,11 +14,12 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; import java.util.Date; +import java.util.Objects; /** * @author 言曌 @@ -34,18 +36,29 @@ public class CommentController { private ArticleService articleService; /** - * '添加评论 + * 添加评论 * * @param request * @param comment */ @RequestMapping(value = "/comment", method = {RequestMethod.POST}) - public JsonResult insertComment(HttpServletRequest request, Comment comment) { + public JsonResult insertComment(HttpServletRequest request, Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + if (user == null) { + return new JsonResult().fail("请先登录"); + } + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId()); + if (article == null) { + return new JsonResult().fail("文章不存在"); + } + + //添加评论 + comment.setCommentUserId(user.getUserId()); comment.setCommentCreateTime(new Date()); comment.setCommentIp(MyUtils.getIpAddr(request)); - if (request.getSession().getAttribute("user") != null) { - comment.setCommentRole(Role.ADMIN.getValue()); + if (Objects.equals(user.getUserId(), article.getArticleUserId())) { + comment.setCommentRole(Role.OWNER.getValue()); } else { comment.setCommentRole(Role.VISITOR.getValue()); } @@ -53,13 +66,13 @@ public JsonResult insertComment(HttpServletRequest request, Comment comment) { //过滤字符,防止XSS攻击 comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent())); - comment.setCommentAuthorName(HtmlUtil.escape(comment.getCommentAuthorName())); - comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail())); - comment.setCommentAuthorUrl(HtmlUtil.escape(comment.getCommentAuthorUrl())); + + comment.setCommentAuthorName(user.getUserNickname()); + comment.setCommentAuthorEmail(user.getUserEmail()); + comment.setCommentAuthorUrl(user.getUserUrl()); try { commentService.insertComment(comment); //更新文章的评论数 - Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId()); articleService.updateCommentCount(article.getArticleId()); } catch (Exception e) { e.printStackTrace(); diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/IndexController.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/IndexController.java index 474ab333..040e3e73 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/IndexController.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/controller/home/IndexController.java @@ -62,7 +62,7 @@ public String index(@RequestParam(required = false, defaultValue = "1") Integer List allTagList = tagService.listTag(); model.addAttribute("allTagList", allTagList); //最新评论 - List recentCommentList = commentService.listRecentComment(10); + List recentCommentList = commentService.listRecentComment(null, 10); model.addAttribute("recentCommentList", recentCommentList); model.addAttribute("pageUrlPrefix", "/article?pageIndex"); return "Home/index"; @@ -91,7 +91,7 @@ public String search( List
mostCommentArticleList = articleService.listArticleByCommentCount(8); model.addAttribute("mostCommentArticleList", mostCommentArticleList); //最新评论 - List recentCommentList = commentService.listRecentComment(10); + List recentCommentList = commentService.listRecentComment(null, 10); model.addAttribute("recentCommentList", recentCommentList); model.addAttribute("pageUrlPrefix", "/search?pageIndex"); return "Home/Page/search"; @@ -103,6 +103,13 @@ public String NotFound(@RequestParam(required = false) String message, Model mod return "Home/Error/404"; } + + @RequestMapping("/403") + public String Page403(@RequestParam(required = false) String message, Model model) { + model.addAttribute("message", message); + return "Home/Error/403"; + } + @RequestMapping("/500") public String ServerError(@RequestParam(required = false) String message, Model model) { model.addAttribute("message", message); diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Comment.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Comment.java index 6f856947..31f17798 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Comment.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/Comment.java @@ -7,10 +7,11 @@ /** * 文章评论 + * * @author liuyanzhao */ @Data -public class Comment implements Serializable{ +public class Comment implements Serializable { private static final long serialVersionUID = -1038897351672911219L; private Integer commentId; @@ -42,9 +43,13 @@ public class Comment implements Serializable{ */ private Integer commentRole; + /** + * 评论用户ID + */ + private Integer commentUserId; + /** * 非数据库字段 */ private Article article; - } \ No newline at end of file diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/User.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/User.java index 88544a9d..326ef3c4 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/User.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/entity/User.java @@ -33,6 +33,11 @@ public class User implements Serializable{ private Integer userStatus; + /** + * 用户角色:admin/user + */ + private String userRole; + /** * 文章数量(不是数据库字段) */ diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/Role.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/Role.java index d4096a6b..a95b01ff 100644 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/Role.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/Role.java @@ -7,8 +7,8 @@ public enum Role { - ADMIN(1, "博主"), - VISITOR(0, "访客"); + OWNER(1, "博主"), + VISITOR(0, "其他用户"); private Integer value; diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/UserRole.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/UserRole.java new file mode 100644 index 00000000..3de0d5a0 --- /dev/null +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/enums/UserRole.java @@ -0,0 +1,40 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * 角色枚举 + * + * @author 言曌 + * @date 2021/2/25 2:31 下午 + */ + +public enum UserRole { + + ADMIN("admin", "管理员"), + + USER("user", "用户"); + + private String value; + + private String message; + + UserRole(String value, String message) { + this.value = value; + this.message = message; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/AdminInterceptor.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/AdminInterceptor.java new file mode 100755 index 00000000..7c151209 --- /dev/null +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/AdminInterceptor.java @@ -0,0 +1,45 @@ +package com.liuyanzhao.ssm.blog.interceptor; + +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Objects; + + +/** + * @author liuyanzhao + */ +@Component +public class AdminInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws IOException { + //这里可以根据session的用户来判断角色的权限,根据权限来转发不同的页面 + User user = (User) request.getSession().getAttribute("user"); + if (user == null) { + response.sendRedirect("/login"); + return false; + } + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return false; + } + return true; + } + + @Override + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { + + } + + @Override + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { + + } +} + diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/LoginInterceptor.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/LoginInterceptor.java new file mode 100755 index 00000000..1201d869 --- /dev/null +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/LoginInterceptor.java @@ -0,0 +1,39 @@ +package com.liuyanzhao.ssm.blog.interceptor; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +/** + * @author liuyanzhao + */ +@Component +public class LoginInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws IOException { + //这里可以根据session的用户来判断角色的权限,根据权限来转发不同的页面 + if(request.getSession().getAttribute("user") == null) { + response.sendRedirect("/login"); + return false; + } + + return true; + } + + @Override + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { + + } + + @Override + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { + + } +} + diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/SecurityInterceptor.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/SecurityInterceptor.java index 6b03d4aa..5bf2a7bb 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/SecurityInterceptor.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/interceptor/SecurityInterceptor.java @@ -23,6 +23,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons response.sendRedirect("/login"); return false; } + return true; } diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/ArticleMapper.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/ArticleMapper.java index ed86da42..2beb1d42 100644 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/ArticleMapper.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/ArticleMapper.java @@ -23,6 +23,14 @@ public interface ArticleMapper { */ Integer deleteById(Integer articleId); + /** + * 根据用户ID删除 + * + * @param userId 用户ID + * @return 影响函数 + */ + Integer deleteByUserId(Integer userId); + /** * 添加文章 * @@ -195,7 +203,7 @@ List
findArticleByCategoryIds(@Param("categoryIds") List categ * @param limit 查询数量 * @return 列表 */ - List
listArticleByLimit(Integer limit); + List
listArticleByLimit(@Param("userId") Integer userId, @Param("limit") Integer limit); /** * 批量删除文章 @@ -204,4 +212,11 @@ List
findArticleByCategoryIds(@Param("categoryIds") List categ * @return 影响行数 */ Integer deleteBatch(@Param("ids") List ids); + + /** + * 获得一个用户的文章id集合 + * @param userId + * @return + */ + List listArticleIdsByUserId(Integer userId); } \ No newline at end of file diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/CommentMapper.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/CommentMapper.java index da94bca6..e8976b35 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/CommentMapper.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/mapper/CommentMapper.java @@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.HashMap; import java.util.List; @Mapper @@ -55,7 +56,16 @@ public interface CommentMapper { * * @return 列表 */ - List listComment(); + List listComment(HashMap criteria); + + + /** + * 获得某个用户收到的评论 + * + * @return 列表 + */ + List getReceiveComment(List articleIds); + /** * 统计评论数 @@ -70,7 +80,8 @@ public interface CommentMapper { * @param limit 查询数量 * @return 列表 */ - List listRecentComment(@Param(value = "limit") Integer limit); + List listRecentComment(@Param(value = "userId") Integer userId, + @Param(value = "limit") Integer limit); /** * 获得评论的子评论 @@ -79,4 +90,22 @@ public interface CommentMapper { * @return 列表 */ List listChildComment(@Param(value = "id") Integer id); + + + /** + * 根据用户ID删除 + * + * @param userId 用户ID + * @return 影响函数 + */ + Integer deleteByUserId(Integer userId); + + + /** + * 根据文章ID删除 + * + * @param articleId 文章ID + * @return 影响函数 + */ + Integer deleteByArticleId(Integer articleId); } \ No newline at end of file diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/ArticleService.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/ArticleService.java index a4495d8e..f489bdce 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/ArticleService.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/ArticleService.java @@ -67,7 +67,7 @@ public interface ArticleService { * @param limit 查询数量 * @return 列表 */ - List
listRecentArticle(Integer limit); + List
listRecentArticle(Integer userId, Integer limit); /** diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/CommentService.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/CommentService.java index f66caae7..17613a83 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/CommentService.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/CommentService.java @@ -4,6 +4,7 @@ import com.liuyanzhao.ssm.blog.entity.Comment; import org.springframework.stereotype.Service; +import java.util.HashMap; import java.util.List; @@ -47,14 +48,20 @@ public interface CommentService { */ PageInfo listCommentByPage( Integer pageIndex, - Integer pageSize); + Integer pageSize, + HashMap criteria); /** - * 获得评论列表 + * 获得某个用户收到的评论 * + * @param pageIndex 第几页开始 + * @param pageSize 一页显示数量 * @return 列表 */ - List listComment(); + PageInfo listReceiveCommentByPage( + Integer pageIndex, + Integer pageSize, + Integer userId); /** @@ -84,7 +91,7 @@ PageInfo listCommentByPage( * @param limit 查询数量 * @return 列表 */ - List listRecentComment(Integer limit); + List listRecentComment(Integer userId, Integer limit); /** * 获得评论的子评论 diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/ArticleServiceImpl.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/ArticleServiceImpl.java index ca3afe1d..c2750481 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/ArticleServiceImpl.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/ArticleServiceImpl.java @@ -3,11 +3,9 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.liuyanzhao.ssm.blog.enums.ArticleCommentStatus; +import com.liuyanzhao.ssm.blog.mapper.*; import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.entity.*; -import com.liuyanzhao.ssm.blog.mapper.ArticleCategoryRefMapper; -import com.liuyanzhao.ssm.blog.mapper.ArticleMapper; -import com.liuyanzhao.ssm.blog.mapper.ArticleTagRefMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -37,6 +35,12 @@ public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleTagRefMapper articleTagRefMapper; + @Autowired + private UserMapper userMapper; + + @Autowired + private CommentMapper commentMapper; + @Override public Integer countArticle(Integer status) { Integer count = 0; @@ -98,8 +102,8 @@ public List
listArticle(HashMap criteria) { } @Override - public List
listRecentArticle(Integer limit) { - return articleMapper.listArticleByLimit(limit); + public List
listRecentArticle(Integer userId, Integer limit) { + return articleMapper.listArticleByLimit(userId, limit); } @Override @@ -140,10 +144,15 @@ public void deleteArticleBatch(List ids) { } @Override + @Transactional(rollbackFor = Exception.class) public void deleteArticle(Integer id) { articleMapper.deleteById(id); // 删除分类关联 articleCategoryRefMapper.deleteByArticleId(id); + // 删除标签管理 + articleTagRefMapper.deleteByArticleId(id); + // 删除评论 + commentMapper.deleteByArticleId(id); } @@ -161,6 +170,8 @@ public PageInfo
pageArticle(Integer pageIndex, categoryList.add(Category.Default()); } articleList.get(i).setCategoryList(categoryList); + + articleList.get(i).setUser(userMapper.getUserById(articleList.get(i).getArticleUserId())); // //封装TagList // List tagList = articleTagRefMapper.listTagByArticleId(articleList.get(i).getArticleId()); // articleList.get(i).setTagList(tagList); diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/CommentServiceImpl.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/CommentServiceImpl.java index e2fb3bb4..20f9015d 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/CommentServiceImpl.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/CommentServiceImpl.java @@ -13,6 +13,8 @@ import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -64,11 +66,11 @@ public Comment getCommentById(Integer id) { } @Override - public PageInfo listCommentByPage(Integer pageIndex, Integer pageSize) { + public PageInfo listCommentByPage(Integer pageIndex, Integer pageSize, HashMap criteria) { PageHelper.startPage(pageIndex, pageSize); List commentList = null; try { - commentList = commentMapper.listComment(); + commentList = commentMapper.listComment(criteria); for (int i = 0; i < commentList.size(); i++) { Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); commentList.get(i).setArticle(article); @@ -81,15 +83,23 @@ public PageInfo listCommentByPage(Integer pageIndex, Integer pageSize) } @Override - public List listComment() { - List commentList = null; + public PageInfo listReceiveCommentByPage(Integer pageIndex, Integer pageSize, Integer userId) { + PageHelper.startPage(pageIndex, pageSize); + List commentList = new ArrayList<>(); try { - commentList = commentMapper.listComment(); + List articleIds = articleMapper.listArticleIdsByUserId(userId); + if (articleIds != null && articleIds.size() > 0) { + commentList = commentMapper.getReceiveComment(articleIds); + for (int i = 0; i < commentList.size(); i++) { + Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); + commentList.get(i).setArticle(article); + } + } } catch (Exception e) { e.printStackTrace(); - log.error("获得评论列表失败:cause:{}", e); + log.error("分页获得评论失败,pageIndex:{}, pageSize:{}, cause:{}", pageIndex, pageSize, e); } - return commentList; + return new PageInfo<>(commentList); } @Override @@ -125,10 +135,10 @@ public Integer countComment() { } @Override - public List listRecentComment(Integer limit) { + public List listRecentComment(Integer userId, Integer limit) { List commentList = null; try { - commentList = commentMapper.listRecentComment(limit); + commentList = commentMapper.listRecentComment(userId, limit); for (int i = 0; i < commentList.size(); i++) { Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); commentList.get(i).setArticle(article); diff --git a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/UserServiceImpl.java b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/UserServiceImpl.java index 5fdb994e..5fe8e2f5 100755 --- a/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/UserServiceImpl.java +++ b/ForestBlog/src/main/java/com/liuyanzhao/ssm/blog/service/impl/UserServiceImpl.java @@ -1,11 +1,14 @@ package com.liuyanzhao.ssm.blog.service.impl; import com.liuyanzhao.ssm.blog.mapper.ArticleMapper; +import com.liuyanzhao.ssm.blog.mapper.CommentMapper; import com.liuyanzhao.ssm.blog.mapper.UserMapper; import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; @@ -25,6 +28,11 @@ public class UserServiceImpl implements UserService { @Autowired private ArticleMapper articleMapper; + @Autowired + private ArticleService articleService; + @Autowired + private CommentMapper commentMapper; + @Override public List listUser() { List userList = userMapper.listUser(); @@ -46,8 +54,19 @@ public void updateUser(User user) { } @Override + @Transactional(rollbackFor = Exception.class) public void deleteUser(Integer id) { + // 删除用户 userMapper.deleteById(id); + // 删除评论 + commentMapper.deleteByUserId(id); + // 删除文章 + List articleIds = articleMapper.listArticleIdsByUserId(id); + if (articleIds != null && articleIds.size() > 0) { + for (Integer articleId : articleIds) { + articleService.deleteArticle(articleId); + } + } } @Override diff --git a/ForestBlog/src/main/resources/db.properties b/ForestBlog/src/main/resources/db.properties index 8e005222..cec7879c 100755 --- a/ForestBlog/src/main/resources/db.properties +++ b/ForestBlog/src/main/resources/db.properties @@ -1,5 +1,5 @@ #MySQL mysql.driver=com.mysql.cj.jdbc.Driver -mysql.url=jdbc:mysql://127.0.0.1:3306/forest_blog?useUnicode=true&characterEncoding=utf8 +mysql.url=jdbc:mysql://127.0.0.1:3306/forest_blog?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC mysql.username=root mysql.password=123456 diff --git a/ForestBlog/src/main/resources/mapper/ArticleMapper.xml b/ForestBlog/src/main/resources/mapper/ArticleMapper.xml index df5ed01b..f80c73cf 100644 --- a/ForestBlog/src/main/resources/mapper/ArticleMapper.xml +++ b/ForestBlog/src/main/resources/mapper/ArticleMapper.xml @@ -82,6 +82,10 @@ + + DELETE FROM + WHERE article_user_id = #{userId} + @@ -321,7 +325,21 @@ article_update_time, article_is_comment, article_status, article_order FROM + + + article_user_id = #{userId} + + ORDER BY article_id DESC - LIMIT #{value} + LIMIT #{limit} + + + diff --git a/ForestBlog/src/main/resources/mapper/CommentMapper.xml b/ForestBlog/src/main/resources/mapper/CommentMapper.xml index 9d9d0382..266d4d17 100755 --- a/ForestBlog/src/main/resources/mapper/CommentMapper.xml +++ b/ForestBlog/src/main/resources/mapper/CommentMapper.xml @@ -15,6 +15,7 @@ + comment @@ -22,7 +23,7 @@ comment_id, comment_pid, comment_pname, comment_article_id, comment_author_name, comment_author_email, comment_author_url, comment_author_avatar, comment_content, comment_agent, - comment_ip,comment_create_time, comment_role + comment_ip,comment_create_time, comment_role,comment_user_id + + @@ -107,7 +141,11 @@ FROM - WHERE comment_role = 0 + + + comment_user_id = #{userId} + + ORDER BY comment_id DESC LIMIT #{limit} diff --git a/ForestBlog/src/main/resources/mapper/UserMapper.xml b/ForestBlog/src/main/resources/mapper/UserMapper.xml index 07c53bf3..aab46d88 100755 --- a/ForestBlog/src/main/resources/mapper/UserMapper.xml +++ b/ForestBlog/src/main/resources/mapper/UserMapper.xml @@ -13,13 +13,14 @@ + user user_id, user_name, user_pass, user_nickname, user_email, user_url, user_avatar, - user_last_login_ip, user_register_time, user_last_login_time, user_status + user_last_login_ip, user_register_time, user_last_login_time, user_status, user_role + @@ -45,6 +46,7 @@ + @@ -56,6 +58,9 @@ +
用户 标题 所属分类 状态
+ ${a.user.userNickname} + diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/index.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/index.jsp index 6a4c3834..f4044978 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/index.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/index.jsp @@ -67,9 +67,13 @@ 回复 - | - 编辑 - + + + | + 编辑 + + + | 删除 diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/reply.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/reply.jsp index fead6121..11f84bde 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/reply.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Comment/reply.jsp @@ -35,7 +35,6 @@ -
@@ -44,24 +43,6 @@
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Public/framework.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Public/framework.jsp index 2fbe88b5..0ac292d5 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Public/framework.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/Public/framework.jsp @@ -48,7 +48,7 @@ ${sessionScope.user.userName}
-
基本资料
+
基本资料
  • @@ -60,7 +60,9 @@
    -
    @@ -120,7 +149,7 @@
  • diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/editProfile.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/editProfile.jsp new file mode 100755 index 00000000..952a4072 --- /dev/null +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/editProfile.jsp @@ -0,0 +1,147 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> +<%@ taglib prefix="rapid" uri="http://www.rapid-framework.org.cn/rapid" %> + + + - 编辑个人信息 + + + + + + + +
    + + 首页 + 个人信息 + 编辑用户信息 + +
    +

    +
    + +
    + +
    +
    +
    + +

    +
    + + +
    +
    +
    +
    + +
    + +
    +
    +
    +
    + +
    + +
    +
    +
    +
    + +
    + +
    +
    +
    +
    + +
    + +
    +
    +
    +
    + +
    + +
    +
    +
    + +
    + checked> + checked> +
    +
    +
    +
    + + +
    +
    +
    + + +
    + + + + + +<%@ include file="../Public/framework.jsp" %> diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/profile.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/profile.jsp index 3c870f57..828d9a74 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/profile.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/User/profile.jsp @@ -43,11 +43,10 @@

    -
    -
    - 编辑 + 编辑
    diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/login.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/login.jsp index c4071521..529c3dc6 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/login.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/login.jsp @@ -105,8 +105,7 @@ wp_attempt_focus(); if(typeof wpOnload=='function')wpOnload(); - -

    ← 返回到风吟博客

    +

    ← 返回到风吟博客 | 注册

    diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Admin/register.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/register.jsp new file mode 100755 index 00000000..7a6a2c9a --- /dev/null +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Admin/register.jsp @@ -0,0 +1,165 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8" %> + + + + + + + + + + ${options.optionSiteTitle} ‹ 注册 + + + + + + + + + + + + + +
    +

    ${options.optionSiteTitle}

    + <% + String username = ""; + String password = ""; + //获取当前站点的所有Cookie + Cookie[] cookies = request.getCookies(); + for (int i = 0; i < cookies.length; i++) {//对cookies中的数据进行遍历,找到用户名、密码的数据 + if ("username".equals(cookies[i].getName())) { + username = cookies[i].getValue(); + } else if ("password".equals(cookies[i].getName())) { + password = cookies[i].getValue(); + } + } + %> + +

    + +

    +

    + +

    +

    + +

    +

    + +

    +

    + +

    +

    + +

    + + +

    ← 返回到风吟博客 | 登录

    + +
    + + +
    + + + + + + diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Home/Error/403.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Error/403.jsp new file mode 100755 index 00000000..0a34cc0e --- /dev/null +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Error/403.jsp @@ -0,0 +1,62 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8" %> + + + + + + 403 没有权限操作 + + + +
    +
    +
    +

    + 404很抱歉,您没有权限操作! +

    +
    +
    +

    可能原因:

    +
      +
    1. 访问了其他用户的数据
    2. +
    3. 系统异常
    4. +
    5. 输入的网址不正确
    6. +
    +
    +
    +

    可以尝试:

    +
      +
    1. 返回首页
    2. +
    3. 留言反馈
    4. +
    5. + 联系站长 +
    6. +
    +
    +
    +
    +
    +
    + + \ No newline at end of file diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Home/Page/articleDetail.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Page/articleDetail.jsp index 1656618f..a1b48e71 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Home/Page/articleDetail.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Page/articleDetail.jsp @@ -298,21 +298,18 @@ 取消回复 + + 您未登录,登录后才能评论,前往登录 + +
    - 言曌 登录者:${sessionScope.user.userNickname}
    登出 - - - -

    @@ -321,28 +318,6 @@

    - - -

    - - -

    - -

    - - -

    -

    @@ -384,7 +359,7 @@   - + 删除 - $(document).ready(function () { - if ($('#author_name').val() == '') { - var author = localStorage.getItem("author"); - $("#author_name").val(author == 'undefined' ? '' : author); - } - if ($('#author_email').val() == '') { - var email = localStorage.getItem("email"); - $("#author_email").val(email == 'undefined' ? '' : email); - } - if ($('#author_url').val() == '') { - var url = localStorage.getItem("url"); - $("#author_url").val(url == 'undefined' ? '' : url); - } - }); var articleId = $("#articleDetail").attr("data-id"); increaseViewCount(articleId); diff --git a/ForestBlog/src/main/webapp/WEB-INF/view/Home/Public/part/header.jsp b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Public/part/header.jsp index be62a920..f4be5852 100755 --- a/ForestBlog/src/main/webapp/WEB-INF/view/Home/Public/part/header.jsp +++ b/ForestBlog/src/main/webapp/WEB-INF/view/Home/Public/part/header.jsp @@ -13,13 +13,13 @@