学生公寓管理系统
简介
基于 SpringBoot + Vue 的学生公寓管理系统,自定义了权限拦截器进行权限认证与授权,使用 aop+log4j 进行日志记录,使用 reids 作为缓存,使用 mysql 作为数据库,使用 druid 作为数据库连接池,使用 jwt 作为前后端状态交互信息,使用 websocket 进行通知的实时推送。
功能
-
登录注销,修改密码个人信息
-
宿舍管理
-
学生管理
-
班级管理
-
宿舍楼管理
-
维修记录
-
用户管理
-
菜单管理
-
角色管理
-
日志
-
通知管理
-
退宿审核
代码
自定义权限拦截
@Component
public class SecurityInterceptor implements HandlerInterceptor {
private final RedisUtil redisUtil;
private final SystemFunctionService systemFunctionService;
private final SystemRoleService systemRoleService;
private static final Map<Match, Validate> VALIDATE_MAP = new HashMap<>();
static {
VALIDATE_MAP.put(Match.HAS_ANY, (userPermission, methodPermission) -> {
for (String up : userPermission) {
for (String mp : methodPermission) {
if (up.equalsIgnoreCase(mp)) {
return true;
}
}
}
return false;
});
VALIDATE_MAP.put(Match.HAS_ALL, (userPermission, methodPermission) -> {
int vote = 0;
for (String up : userPermission) {
for (String mp : methodPermission) {
if (up.equalsIgnoreCase(mp)) {
vote++;
}
}
}
return vote == methodPermission.length;
});
}
public SecurityInterceptor(RedisUtil redisUtil, SystemFunctionService systemFunctionService, SystemRoleService systemRoleService) {
this.redisUtil = redisUtil;
this.systemFunctionService = systemFunctionService;
this.systemRoleService = systemRoleService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//获取请求的方法
HandlerMethod handlerMethod;
if (handler instanceof HandlerMethod) {
handlerMethod = (HandlerMethod) handler;
} else {
//404
return true;
}
Method method = handlerMethod.getMethod();
//获取请求方法所需的权限
String[] requiredPermissions;
Match match;
if (method.isAnnotationPresent(RequirePermission.class)) {
RequirePermission hasPermission = method.getAnnotation(RequirePermission.class);
requiredPermissions = hasPermission.permissions();
match = hasPermission.matchType();
} else {
//方法不需要权限(无 RequirePermission 注解)
return true;
}
String token = request.getHeader(Constant.HEADER_TOKEN);
Long id = redisUtil.get(token);
//获取该用户的权限
List<SystemRole> roleList = systemRoleService.listByUserId(id);
Set<String> permissions;
if (roleList.size() == 0) {
permissions = new HashSet<>();
} else {
permissions = systemFunctionService.getPermission(roleList);
}
//验证权限
if (VALIDATE_MAP.get(match).validate(permissions, requiredPermissions)) {
return true;
}
//权限验证失败
throw new HttpException(HttpCode.HAS_NO_PERMISSIONS, "没有权限,请联系管理员");
}
private interface Validate {
/**
* 验证权限
* @param userPermission 用户拥有的权限
* @param methodPermission 方法需要的权限
* @return 是否通过
*/
Boolean validate(Set<String> userPermission, String[] methodPermission);
}
}
实现通知消息的推送
@Component
@ServerEndpoint("/ws/{name}")
public class WebSocket {
private Session session;
private String name;
public final static Map<String,WebSocket> WEB_SOCKET_SET = new ConcurrentHashMap<>();
@OnOpen
public void onOpen(Session session,@PathParam(value = "name") String name){
this.session = session;
this.name = name;
WEB_SOCKET_SET.put(name,this);
}
@OnClose
public void onClose(){
WEB_SOCKET_SET.remove(name);
}
@OnMessage
public void onMessage(String message) throws JsonProcessingException {
Message m = new ObjectMapper().readValue(message, Message.class);
System.out.println(m);
}
/**
* 发送消息
* @param userId 目标用户id
* @param message 消息内容
* @param systemUserService 。
*/
public static boolean sendMessage(Long userId, Message message, SystemUserService systemUserService) {
SystemUser systemUser = systemUserService.get(userId).orElseThrow(() -> new HttpException(HttpCode.FAILED, "用户不存在"));
if (WEB_SOCKET_SET.containsKey(systemUser.getLoginName())) {
ObjectMapper objectMapper = new ObjectMapper();
try {
WEB_SOCKET_SET.get(systemUser.getLoginName()).session.getBasicRemote()
.sendText(objectMapper.writeValueAsString(message));
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
}
示例
登录
管理员主页
宿舍管理
学生管理
班级管理
宿舍楼管理
维修记录
用户管理
角色管理
菜单管理
日志记录
消息
审核