基于javaweb+mysql的springboot个人博客系统(java+springboot+springsecurity+thymeleaf+html+mysql+redis)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot个人博客系统(java+springboot+springsecurity+thymeleaf+html+mysql+redis)
项目介绍
本系统是一个个人博客的管理系统,用户可以注册账号并登陆系统查看发布的文章并进行阅读评论等 本系统主要有三种用户: 1、游客角色主要功能: 没有登陆的用户,可以查询文章,但是无法进行评论,系统中的所有文章可以通过内容搜索,可以通过分类搜索也可以通过标签就行搜索 2、注册用户主要功能 可以对所有的文章进行阅读以及评论等功能,可以对文章进行赞赏(支付宝或者微信付钱) 3、管理员主要功能: 拥有注册用户的所有权限,管理分类以及发布文章和管理标签等,友链管理,时间轴管理等
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 4.数据库:MySql 5.7版本; 5.是否Maven项目:是;
技术栈
SpringBoot+SpringSecurity+Mybatis+Mysql+Redis+Thymeleaf+JQuery
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,导入成功后请执行maven clean;maven install命令,然后运行; 3. 将项目中application.yml配置文件中的数据库配置改为自己的配置; 4. 运行项目,项目运行成功后,访问地址localhost:8080 管理员账号密码:zhangpeng98aliyun.com/mt2680324 用户账号密码:zhangpengaliyun.com/mt2680324
@Component
public class ImageCodeAuthenticationFilter extends OncePerRequestFilter {
@Autowired
private AuthenticationFailureHandler authenticationFailureHandler;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//判断当前请求是否是登录请求
if (request.getRequestURI ().contains ("/securityLogin")) {
try {
//获取用户输入的验证码
String imageCode = request.getParameter ("vercode");
//获取系统生成的验证码
String key = (String) request.getSession ().getAttribute ("code");
if (StringUtils.isEmpty (imageCode)) {
throw new ImageCodeException ("验证码不能为空!");
}
if (!imageCode.equalsIgnoreCase (key)) {
throw new ImageCodeException ("验证码输入错误!");
}
String email = request.getParameter ("email");
if (StringUtils.isEmpty (email)) {
throw new ImageCodeException ("邮箱不能为空!");
}
String password = request.getParameter ("password");
if (StringUtils.isEmpty (password)) {
throw new ImageCodeException ("密码不能为空!");
}
} catch (AuthenticationException e) {
authenticationFailureHandler.onAuthenticationFailure (request, response, e);
return;
}
}
filterChain.doFilter (request, response);
}
}
public String deleteById(Integer id, RedirectAttributes attributes){
try {
messageService.deleteById(id);
attributes.addFlashAttribute ("message","删除成功!");
} catch (Exception e) {
attributes.addFlashAttribute ("message","删除失败,服务器异常!");
e.printStackTrace ();
}
return "redirect:showAll";
}
}
@Controller
@RequestMapping("/admin/syslog")
public class AdminSysLogController {
@Autowired
private SysLogService sysLogService;
@RequestMapping("/showAll")
public String showAll(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
@RequestParam(name = "nickname", required = false, defaultValue = "") String nickname, Model model) {
show (pageNum, pageSize, nickname, model);
model.addAttribute ("nickname", nickname);
return "admin/syslogs";
}
//注册
@ResponseBody
@RequestMapping("/register")
public Map<String, Object> register(UserInfo userInfo, HttpServletRequest request) {
//用于存储返回响应的信息
Map<String, Object> map = new HashMap<> ();
userInfo.setCreateTime (new Date ());
//默认头像
userInfo.setHeadPortrait ("/upload/default.jpg");
userInfo.setStatus (true);
userInfo.setUpdateTime (new Date ());
boolean register = userService.register (userInfo);
if (register) {
// String message = "你好," + userInfo.getNickname () + "! <a href='http://59.110.60.4:80/user/active?email=" + userInfo.getEmail () + "'>点击激活</a>";
// SendEmailUtil.sendMessage (userInfo.getEmail (), message);
map.put ("flag", true);
} else {
map.put ("flag", false);
map.put ("reg_error", "邮箱已存在");
}
return map;
}
//邮箱激活
@RequestMapping("/active")
public String active(String email, RedirectAttributes attributes) {
UserInfo userInfo = userService.findByEmail (email);
Boolean status = userInfo.getStatus ();
try {
userService.updateStatus (email, !status);
attributes.addFlashAttribute ("flag", true);
attributes.addFlashAttribute ("msg", "激活成功!");
} catch (Exception e) {
e.printStackTrace ();
attributes.addFlashAttribute ("flag", false);
attributes.addFlashAttribute ("msg", "激活失败!服务器异常");
}
return "redirect:login";
}
return "redirect:showAll";
}
}
@Aspect
@Component
public class BlogAspectLog {
@Autowired
private SysLogService sysLogService;
private Date visitTime; //访问时间
private Class clazz; //访问的类
private Method method; //访问的方法
@Pointcut("execution(* com.zp.blog.web.*.*.*(..)) && !execution(* com.zp.blog.web.back.AdminSysLogController.*(..)) && !execution(* com.zp.blog.web.comment.*.*(..))")
public void pt() {
}
@Before("pt()")
public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException {
@RequestMapping("/getHotBlogs")
public String getHotBlogs(Model model){
List<Blog> blogs = blogService.selectHotBlogs (); //根据查看的次数降序获取4个
model.addAttribute ("blogs",blogs);
return "commons :: hotBlogs";
}
}
@Controller
@RequestMapping("/admin/type")
public class AdminTypeController {
@Autowired
private TypeService typeService;
@RequestMapping("/showAll")
public ModelAndView showAll(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "name", required = false, defaultValue = "") String name){
ModelAndView mv = new ModelAndView ();
//调用service层获取数据
List<Type> types = typeService.selectAllByLikeName (pageNum, pageSize, name);
//使用pageInfo包装查询后的结果,封装了详细的查询数据,其中参数5是页码导航连续显示的页数
PageInfo<Type> pageInfo = new PageInfo<> (types,5);
mv.addObject ("pageInfo",pageInfo);
mv.addObject ("name",name);
} catch (IOException e) {
e.printStackTrace ();
map.put ("code", 1);
}
return map;
}
//注册
@ResponseBody
@RequestMapping("/register")
public Map<String, Object> register(UserInfo userInfo, HttpServletRequest request) {
//用于存储返回响应的信息
Map<String, Object> map = new HashMap<> ();
userInfo.setCreateTime (new Date ());
//默认头像
userInfo.setHeadPortrait ("/upload/default.jpg");
userInfo.setStatus (true);
userInfo.setUpdateTime (new Date ());
boolean register = userService.register (userInfo);
if (register) {
// String message = "你好," + userInfo.getNickname () + "! <a href='http://59.110.60.4:80/user/active?email=" + userInfo.getEmail () + "'>点击激活</a>";
// SendEmailUtil.sendMessage (userInfo.getEmail (), message);
map.put ("flag", true);
} else {
map.put ("flag", false);
map.put ("reg_error", "邮箱已存在");
}
return map;
}
//邮箱激活
@RequestMapping("/active")
public String active(String email, RedirectAttributes attributes) {
UserInfo userInfo = userService.findByEmail (email);
Boolean status = userInfo.getStatus ();
try {
public void show(Integer pageNum, Integer pageSize, Model model){
List<Time> times = timeService.selectAll (pageNum, pageSize);
PageInfo<Time> pageInfo = new PageInfo<> (times, 8);
model.addAttribute ("pageInfo", pageInfo);
}
@RequestMapping("/showAll/search")
public String search(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize, Model model){
show (pageNum, pageSize, model);
return "admin/times :: timeList";
}
@RequestMapping("/toAdd")
public String toAdd(){
return "admin/times-input";
}
@RequestMapping("/add")
public String add(Time time, RedirectAttributes attributes){
time.setCreateTime (new Date ());
try {
timeService.save (time);
attributes.addFlashAttribute ("message","添加成功!");
} catch (Exception e) {
e.printStackTrace ();
attributes.addFlashAttribute ("message","服务器出现异常");
}
return "redirect:showAll";
}
@RequestMapping("/toUpdate")
public String toUpdate(Integer id, Model model){
Time time = timeService.selectById (id);
model.addAttribute ("time",time);
return "admin/times-update";
}
@RequestMapping("/update")
public String update(Time time, RedirectAttributes attributes){
try {
timeService.update (time);
attributes.addFlashAttribute ("message","修改成功!");
} catch (Exception e) {
e.printStackTrace ();
attributes.addFlashAttribute ("message","服务器出现异常");
}
return "redirect:showAll";
@RequestMapping("/toUpdatePassword")
public String toUpdatePassword(){
return "user/update-password";
}
//更新密码
@RequestMapping("/updatePassword")
@ResponseBody
public Map<String, Object> updatePassword(String password){
Map<String, Object> map = new HashMap<> ();
UserInfo user = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
Long id = user.getId ();
try {
userService.updatePassword (id, password);
user = null;
map.put ("flag",true);
map.put ("msg","修改成功");
} catch (Exception e) {
map.put ("flag",false);
map.put ("msg","修改失败!服务器异常");
e.printStackTrace ();
}
return map;
}
}
@Controller
@RequestMapping("/admin/blog")
public class AdminBlogController {
List<Blog> blogs = blogService.search(pageNum, pageSize, searchContent);
PageInfo<Blog> pageInfo = new PageInfo<> (blogs, 5);
model.addAttribute ("pageInfo",pageInfo);
model.addAttribute ("searchContent",searchContent);
return "search";
}
@RequestMapping("/search/page")
public String searchContentByPage(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize ,
@RequestParam(name = "searchContent", required = false, defaultValue = "") String searchContent,
Model model){
List<Blog> blogs = blogService.search(pageNum, pageSize, searchContent);
PageInfo<Blog> pageInfo = new PageInfo<> (blogs, 5);
model.addAttribute ("pageInfo",pageInfo);
return "search::blogList";
}
@RequestMapping("/details")
public String details(Long id, Model model){
Blog blog = blogService.selectByIdOfFront (id);
model.addAttribute ("blog",blog);
return "blog";
}
@RequestMapping("/getHotBlogs")
public String getHotBlogs(Model model){
List<Blog> blogs = blogService.selectHotBlogs (); //根据查看的次数降序获取4个
model.addAttribute ("blogs",blogs);
return "commons :: hotBlogs";
}
}
} else {
attributes.addFlashAttribute ("message","删除失败,有博客属于该标签,不能删除!");
}
return "redirect:showAll";
}
}
@Controller
@RequestMapping("/admin/link")
public class AdminLinkController {
@Autowired
private LinkService linkService;
@RequestMapping("/showAll")
public String showAll(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "name", required = false, defaultValue = "") String name, Model model) {
show (pageNum, pageSize, name, model);
return "admin/links";
}
public void show(Integer pageNum, Integer pageSize, String name, Model model) {
List<Link> links = linkService.selectAll (pageNum, pageSize, name);
PageInfo<Link> pageInfo = new PageInfo<> (links, 8);
model.addAttribute ("pageInfo", pageInfo);
}
@RequestMapping("/showAll/search")
public String search(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "name", required = false, defaultValue = "") String name, Model model) {
show (pageNum, pageSize, name, model);
}
@Component
public class ImageCodeAuthenticationFilter extends OncePerRequestFilter {
@Autowired
private AuthenticationFailureHandler authenticationFailureHandler;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//判断当前请求是否是登录请求
if (request.getRequestURI ().contains ("/securityLogin")) {
try {
//获取用户输入的验证码
String imageCode = request.getParameter ("vercode");
//获取系统生成的验证码
String key = (String) request.getSession ().getAttribute ("code");
if (StringUtils.isEmpty (imageCode)) {
throw new ImageCodeException ("验证码不能为空!");
}
if (!imageCode.equalsIgnoreCase (key)) {
throw new ImageCodeException ("验证码输入错误!");
}
String email = request.getParameter ("email");
if (StringUtils.isEmpty (email)) {
throw new ImageCodeException ("邮箱不能为空!");
}
String password = request.getParameter ("password");
if (StringUtils.isEmpty (password)) {
throw new ImageCodeException ("密码不能为空!");
if (update){
attributes.addFlashAttribute ("message","修改成功!");
} else {
attributes.addFlashAttribute ("message","修改失败!");
}
return "redirect:showAll";
}
@RequestMapping("/deleteById")
public String deleteById(Integer id, RedirectAttributes attributes){
//调用service层删除标签
Boolean aBoolean = TagService.deleteById (id);
if (aBoolean){
attributes.addFlashAttribute ("message","删除成功!");
} else {
attributes.addFlashAttribute ("message","删除失败,有博客属于该标签,不能删除!");
}
return "redirect:showAll";
}
}
@Controller
@RequestMapping("/admin/link")
// String uri = request.getRequestURI ();
//获取执行时间
Integer executionTime = Math.toIntExact (new Date ().getTime () - visitTime.getTime ());
//获取ip 需要request获取 --》 获取request需要在web.xml中配置Listener RequestContextListener类 在注入HttpServletRequest
//获取request对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ();
HttpServletRequest request = attributes.getRequest ();
String ip = request.getRemoteAddr ();
//获取操作者用户名(两种方式) 1.从上下文中获取登录的对象 2.也可以从request.getSession中获取
SecurityContext context = SecurityContextHolder.getContext ();
// SecurityContext context = (SecurityContext) request.getSession ().getAttribute ("SPRING_SECURITY_CONTEXT");
Object principal = context.getAuthentication ().getPrincipal ();
String nickname = "";
if (principal instanceof UserInfo) {
UserInfo userInfo = (UserInfo) context.getAuthentication ().getPrincipal ();
nickname = userInfo.getNickname ();
} else {
nickname = "还没登录";
}
//封装日志类 SysLog
SysLog sysLog = new SysLog ();
sysLog.setId (UUID.randomUUID ().toString ());
sysLog.setVisitTime (visitTime);
sysLog.setExecutionTime (executionTime);
sysLog.setIp (ip);
sysLog.setUrl (url);
sysLog.setNickname (nickname);
sysLog.setMethod ("[类名]" + clazz.getName () + "[方法名]" + method.getName ());
//调用service
sysLogService.save (sysLog);
}
}
}
//获取登录的用户
UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext ().getAuthentication ().getPrincipal ();
comment.setUserInfo (userInfo);
comment.setCreateTime (new Date ());
if (comment.getParentComment ().getId () == -1){
comment.getParentComment ().setId (null);
}
commentService.saveComment (comment);
return "redirect:showAllCommentByBlogId?id="+comment.getBlog ().getId ();
}
@RequestMapping("/showAllCommentByBlogId")
public String showAllCommentByBlogId(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
@RequestParam(name = "id") Long id, Model model){
List<Comment> comments = commentService.selectByBlogIdAndParentCommentIsNull (pageNum, pageSize, id);
PageInfo<Comment> pageInfo = new PageInfo<> (comments,8);
CommentServiceImpl commentServiceImpl = (CommentServiceImpl) commentService;
List<Comment> commentList = commentServiceImpl.getAll (pageInfo.getList ()); //获取后代评论
pageInfo.setList (commentList); //封装好的顶级评论和后代评论设置给pageInfo的list
model.addAttribute ("pageInfo",pageInfo);
return "blog :: commentList";
}
}
@Controller
@RequestMapping("/front/type")
public class FrontTypeController {
@Autowired
@Controller
@RequestMapping("/admin/blog")
public class AdminBlogController {
@Autowired
private BlogService blogService;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@RequestMapping("/showAll")
public ModelAndView showAll(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "title", required = false, defaultValue = "") String title,
@RequestParam(name = "typeId", required = false, defaultValue = "0") Integer typeId,
@RequestParam(name = "recommend", required = false, defaultValue = "false") Boolean recommend) {
if (recommend == false) {
recommend = null;
}
ModelAndView mv = new ModelAndView ();
//展示所有博客前查询所有类别
List<Type> types = typeService.selectAll ();
mv.addObject ("types", types);
//调用业务层方法
List<Blog> blogs = blogService.selectAll (pageNum, pageSize, title, typeId, recommend);
PageInfo<Blog> pageInfo = new PageInfo<> (blogs, 5);
mv.addObject ("pageInfo", pageInfo);
mv.addObject ("title", title);
mv.addObject ("typeId", typeId);
@RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
@RequestParam(name = "name", required = false, defaultValue = "") String name){
ModelAndView mv = new ModelAndView ();
//调用service层获取数据
List<Tag> tags = TagService.selectAllByLikeName (pageNum, pageSize, name);
//使用pageInfo包装查询后的结果,封装了详细的查询数据,其中参数5是页码导航连续显示的页数
PageInfo<Tag> pageInfo = new PageInfo<> (tags,5);
mv.addObject ("pageInfo",pageInfo);
mv.addObject ("name",name);
mv.setViewName ("admin/tags");
return mv;
}
@RequestMapping("/toAdd")
public String toAdd(Model model){
model.addAttribute ("tag",new Tag ());
return "admin/tags-input";
}
@RequestMapping("/add")
public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
Tag tag1 = TagService.selectByName (tag.getName ());
if (tag1 != null){
result.rejectValue ("name","nameError","不能添加重复的标签名称");
}
if (result.hasErrors ()){
return "admin/Tags-input";
}
//调用service层保存标签
Boolean save = TagService.save (tag);
if (save){
attributes.addFlashAttribute ("message","添加成功!");
} else {
attributes.addFlashAttribute ("message","添加失败!");
}
return "redirect:showAll";
}
@RequestMapping("/toUpdate")
public ModelAndView toUpdate(Integer id){
ModelAndView mv = new ModelAndView ();
Tag tag = TagService.selectById (id);
@RequestMapping("/showAll/search")
public String search(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "6") Integer pageSize,
@RequestParam(name = "content", required = false, defaultValue = "") String content,
@RequestParam(name = "email", required = false, defaultValue = "") String email,
@RequestParam(name = "blogId", required = false, defaultValue = "-1") Long blogId,
Model model) {
if (blogId == -1){
blogId = null;
}
show (pageNum, pageSize, content, email, blogId, model);
return "admin/comments :: commentList";
}
@RequestMapping("/deleteById")
public String deleteById(Integer id, RedirectAttributes attributes){
try {
commentService.deleteById(id);
attributes.addFlashAttribute ("message","删除成功!");
} catch (Exception e) {
attributes.addFlashAttribute ("message","删除失败,服务器异常!");
e.printStackTrace ();
}
return "redirect:showAll";
}
}