Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
saysky committed Oct 15, 2019
1 parent a3ee67e commit 286bdd0
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 14 deletions.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
最新消息,SpringCloud 博客、音乐、商城、论坛系统开发中,开源,[sens-parent](https://github.com/saysky/sens-parent)
# 关于项目
该项目是基于SpringBoot2.x+Shiro+Redis+ElasticSearch实现的博客系统。<br/>
项目在重构中,目标是后台采用Vue Element UI实现,提高用户体验,敬请期待。<br/>
预览地址:http://blog.liuyanzhao.com <br/>
更新详细介绍:https://liuyanzhao.com/9354.html <br/>

# 功能介绍
本博客系统基于 SpringBoot 2.x ,支持快速开发,部署,采用内置服务器 undertow (抛弃 tomcat)。

数据库采用常见关系型数据库 MySQL,ORM 框架是 MyBatis + MyBatis Plus。

采用 Redis 缓存数据,所有数据基本都先走 MySQL 然后走 Redis (部分分页数据和搜索数据除外)。

采用轻量级安全框架 Shiro,目前本站由三种角色(管理员,作者,订阅者),也可以动态添加角色,并绑定权限,权限和后台菜单挂钩。

ElasticSearch 作为本站的站内搜索框架,支持分词检索数据和关键词高亮。

模板引擎采用 FreeMarker,为了考虑 SEO 没有使用 js 框架,后期会考虑将后台改为 Vue 实现,敬请期待。

前台页面还是一如既往的使用本站的模板页面,后台模板采用 AdminLte 的模板(基于BootStrap的)。

本站支持用户登录/注册,OAuth2.0 第三方登录。

# 页面预览
![image](img/1.png)
<hr/>

![image](img/2.png)
<hr/>

![image](img/3.png)
<hr/>

![image](img/4.png)
<hr/>

![image](img/5.png)
<hr/>

![image](img/6.png)
<hr/>

![image](img/7.png)
<hr/>

![image](img/8.png)
<hr/>

![image](img/9.png)
<hr/>

![image](img/10.png)
<hr/>

# 项目结构和数据库
![image](img/11.png)
<hr/>

![image](img/13.png)
<hr/>

# 关于开源
目前仅开源了后端代码,您可以学习SpringBoot2.x,Shiro,Redis等常见用法。
同时该项目已整合了ElasticSearch6.5,学有余力也可以看一下。
同时,这个项目时启动不起来的,因为缺少 Mapper xml 文件,所以说只适合阅读代码。
如果您需要完整版代码,请联系博主:847064370(QQ和微信)。

## 请喝一杯奶茶
请我喝一杯奶茶吧!
插条广告:博主最近有点时间,长期接毕设定制,软件定制,Java问题解决,带新人,一对一讲课等业务 <br/>
具体查看[有偿服务](https://liuyanzhao.com/bulletin/my-service)

![image](https://github.com/saysky/ForestBlog/blob/master/uploads/donate.png)

Binary file added img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
</dependency>



<!-- druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.liuyanzhao.sens.config.shiro;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* URL拦截器
*
* @author 言曌
* @date 2019-10-12 17:56
*/
Expand All @@ -37,7 +46,23 @@ protected boolean onPreHandle(ServletRequest request, ServletResponse response,
}

//没有权限
WebUtils.issueRedirect(request, response, "/403");
if (isAjax((HttpServletRequest) request)) {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("code", 0);
map.put("msg", "没有权限访问");
writer.write(JSONObject.toJSONString(map));
} else {
WebUtils.issueRedirect(request, response, "/403");
}
return false;
}

public static boolean isAjax(HttpServletRequest httpRequest) {
return (httpRequest.getHeader("X-Requested-With") != null
&& "XMLHttpRequest"
.equals(httpRequest.getHeader("X-Requested-With").toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ public ModelAndView defaultErrorHandler(HttpServletRequest request,
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, Object> attributes = new HashMap<String, Object>();
if (e instanceof UnauthorizedException) {
String msg = localeMessageUtil.getMessage("code.admin.common.permission-denied");
attributes.put("msg", msg);
attributes.put("msg", "没有权限");
} else {
attributes.put("msg", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public void updatePassword(Long userId, String password) {
User user = new User();
user.setId(userId);
user.setUserPass(Md5Util.toMd5(password, "sens", 10));
user.setUserPass(new Md5Hash(password, "sens", 10).toString());
userMapper.updateById(user);
redisUtil.del(RedisKeys.USER + userId);

Expand Down Expand Up @@ -208,6 +207,11 @@ public User update(User user) {
basicUserCheck(user);
//2.验证用户名和邮箱是否存在
checkUserNameAndUserName(user);

if(user.getUserPass() != null) {
String userPass = Md5Util.toMd5(user.getUserPass(), "sens", 10);
user.setUserPass(userPass);
}
userMapper.updateById(user);
redisUtil.del(RedisKeys.USER + user.getId());
return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import com.liuyanzhao.sens.model.enums.LogTypeEnum;
import com.liuyanzhao.sens.model.enums.LoginTypeEnum;
import com.liuyanzhao.sens.model.enums.ResultCodeEnum;
import com.liuyanzhao.sens.service.PostService;
import com.liuyanzhao.sens.service.RoleService;
import com.liuyanzhao.sens.service.UserRoleRefService;
import com.liuyanzhao.sens.service.UserService;
import com.liuyanzhao.sens.service.*;
import com.liuyanzhao.sens.utils.LocaleMessageUtil;
import com.liuyanzhao.sens.utils.Md5Util;
import com.liuyanzhao.sens.utils.PageUtil;
Expand All @@ -28,6 +25,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
* <pre>
Expand All @@ -54,6 +52,9 @@ public class UserController extends BaseController {
@Autowired
private PostService postService;

@Autowired
private PermissionService permissionService;

@Autowired
private LocaleMessageUtil localeMessageUtil;

Expand Down Expand Up @@ -222,6 +223,9 @@ public String loginIt(@RequestParam("id") Long userId) {
// freeRealm 无需验证账号密码
UserToken userToken = new UserToken(user.getUserName(), null, LoginTypeEnum.FREE.getValue());
subject.login(userToken);

Set<String> permissionUrls = permissionService.findPermissionUrlsByUserId(user.getId());
subject.getSession().setAttribute("permissionUrls", permissionUrls);
return "redirect:/admin";
}

Expand Down Expand Up @@ -253,8 +257,7 @@ public JsonResult adminChangePass(@ModelAttribute("id") Long userId,
@ModelAttribute("newPass") String newPass) {
User user = userService.get(userId);
if (null != user) {
String password = Md5Util.toMd5(newPass, "sens", 10);
userService.updatePassword(user.getId(), password);
userService.updatePassword(user.getId(), newPass);
}
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.user.update-password-success"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Set;


/**
Expand All @@ -43,12 +44,11 @@ public class FrontOAuthController extends BaseController {
@Autowired
private ThirdAppBindService thirdAppBindService;

@Autowired
private LogService logService;

@Autowired
private LocaleMessageUtil localeMessageUtil;

@Autowired
private PermissionService permissionService;
/**
* 第三方授权后会回调此方法,并将code传过来
*
Expand All @@ -74,6 +74,9 @@ public String oauthByQQ(@RequestParam(value = "code") String code, HttpServletRe
UserToken userToken = new UserToken(user.getUserName(), user.getUserPass(), LoginTypeEnum.FREE.getValue());
try {
subject.login(userToken);

Set<String> permissionUrls = permissionService.findPermissionUrlsByUserId(user.getId());
subject.getSession().setAttribute("permissionUrls", permissionUrls);
} catch (LockedAccountException e) {
e.printStackTrace();
log.error("第三方登录(QQ)免密码登录失败, 账号被锁定, cause:{}", e.getMessage());
Expand Down Expand Up @@ -135,6 +138,9 @@ public String oauthByGitHub(@RequestParam(value = "code") String code,
UserToken userToken = new UserToken(user.getUserName(), user.getUserPass(), LoginTypeEnum.FREE.getValue());
try {
subject.login(userToken);

Set<String> permissionUrls = permissionService.findPermissionUrlsByUserId(user.getId());
subject.getSession().setAttribute("permissionUrls", permissionUrls);
} catch (Exception e) {
e.printStackTrace();
log.error("第三方登录(GitHub)免密码登录失败, cause:{}", e);
Expand Down

0 comments on commit 286bdd0

Please sign in to comment.