mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-15 02:57:10 +08:00
新增:新增系统监控/在线用户功能,并优化部分注释规范
This commit is contained in:
@@ -169,7 +169,20 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',认证失败,无法访问系统资源", request.getRequestURI(), e);
|
||||
String errorMsg = "登录状态已过期,请重新登录";
|
||||
|
||||
String errorMsg;
|
||||
switch (e.getType()) {
|
||||
case NotLoginException.KICK_OUT:
|
||||
errorMsg = "您已被踢下线";
|
||||
break;
|
||||
case NotLoginException.BE_REPLACED_MESSAGE:
|
||||
errorMsg = "您已被顶下线";
|
||||
break;
|
||||
default:
|
||||
errorMsg = "登录状态已过期,请重新登录";
|
||||
break;
|
||||
}
|
||||
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.UNAUTHORIZED.value(), errorMsg);
|
||||
}
|
||||
|
@@ -83,4 +83,29 @@ public class LoginUser implements Serializable {
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 登录 IP
|
||||
*/
|
||||
private String clientIp;
|
||||
|
||||
/**
|
||||
* 登录地点
|
||||
*/
|
||||
private String location;
|
||||
|
||||
/**
|
||||
* 浏览器
|
||||
*/
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
private LocalDateTime loginTime;
|
||||
}
|
||||
|
@@ -78,6 +78,10 @@ public class PageQuery implements Serializable {
|
||||
this.size = DEFAULT_SIZE;
|
||||
}
|
||||
|
||||
public int getPage() {
|
||||
return page < 0 ? DEFAULT_PAGE : page;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析排序条件为 Spring 分页排序实体
|
||||
*
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package top.charles7c.cnadmin.common.model.vo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
@@ -26,6 +27,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
/**
|
||||
* 分页信息
|
||||
@@ -93,4 +95,37 @@ public class PageInfo<V> {
|
||||
pageInfo.setTotal(pageInfo.getTotal());
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于列表数据构建分页信息
|
||||
*
|
||||
* @param page
|
||||
* 页码
|
||||
* @param size
|
||||
* 每页记录数
|
||||
* @param list
|
||||
* 列表数据
|
||||
* @param <V>
|
||||
* 列表数据类型
|
||||
* @return 分页信息
|
||||
*/
|
||||
public static <V> PageInfo<V> build(int page, int size, List<V> list) {
|
||||
PageInfo<V> pageInfo = new PageInfo<>();
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
pageInfo.setTotal(list.size());
|
||||
// 对列表数据进行分页
|
||||
int fromIndex = (page - 1) * size;
|
||||
int toIndex = page * size + size;
|
||||
if (fromIndex > list.size()) {
|
||||
pageInfo.setList(new ArrayList<>());
|
||||
} else if (toIndex >= list.size()) {
|
||||
pageInfo.setList(list.subList(fromIndex, list.size()));
|
||||
} else {
|
||||
pageInfo.setList(list.subList(fromIndex, toIndex));
|
||||
}
|
||||
return pageInfo;
|
||||
}
|
||||
}
|
||||
|
@@ -16,15 +16,24 @@
|
||||
|
||||
package top.charles7c.cnadmin.common.util.helper;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.consts.CacheConstants;
|
||||
import top.charles7c.cnadmin.common.model.dto.LogContext;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.IpUtils;
|
||||
import top.charles7c.cnadmin.common.util.ServletUtils;
|
||||
import top.charles7c.cnadmin.common.util.holder.LogContextHolder;
|
||||
|
||||
/**
|
||||
* 登录助手
|
||||
@@ -42,8 +51,22 @@ public class LoginHelper {
|
||||
* 登录用户信息
|
||||
*/
|
||||
public static void login(LoginUser loginUser) {
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
if (loginUser == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 记录登录信息
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
loginUser.setClientIp(ServletUtil.getClientIP(request));
|
||||
loginUser.setLocation(IpUtils.getCityInfo(loginUser.getClientIp()));
|
||||
loginUser.setBrowser(ServletUtils.getBrowser(request));
|
||||
LogContext logContext = LogContextHolder.get();
|
||||
loginUser.setLoginTime(logContext != null ? logContext.getCreateTime() : LocalDateTime.now());
|
||||
|
||||
// 登录保存用户信息
|
||||
StpUtil.login(loginUser.getUserId());
|
||||
loginUser.setToken(StpUtil.getTokenValue());
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user