refactor(framework): 优化代码结构和性能
This commit is contained in:
+1
@@ -51,6 +51,7 @@ public class KitSpringBeanHolder implements ApplicationContextAware, DisposableB
|
||||
/**
|
||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public static <T> T getBean(String name) {
|
||||
assertContextInjected();
|
||||
return (T) applicationContext.getBean(name);
|
||||
|
||||
+4
-6
@@ -261,13 +261,11 @@ public class KitRedisHelper {
|
||||
return null;
|
||||
}
|
||||
// 如果 value 不是目标类型, 则尝试将其反序列化为 clazz 类型
|
||||
if (!clazz.isInstance(value)) {
|
||||
return JSON.parseObject(value.toString(), clazz);
|
||||
} else if (clazz.isInstance(value)) {
|
||||
boolean isInstance = clazz.isInstance(value);
|
||||
if (isInstance) {
|
||||
return clazz.cast(value);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(value.toString(), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -631,7 +629,7 @@ public class KitRedisHelper {
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().remove(key, values);
|
||||
return count;
|
||||
return count == null ? 0 : count;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return 0;
|
||||
|
||||
+10
-5
@@ -87,6 +87,15 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
|
||||
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
|
||||
List<MediaType> supportMediaTypeList = new ArrayList<>();
|
||||
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
|
||||
FastJsonConfig config = this.getFastJsonConfig();
|
||||
fastJsonConverter.setFastJsonConfig(config);
|
||||
fastJsonConverter.setSupportedMediaTypes(supportMediaTypeList);
|
||||
fastJsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
|
||||
// 将 FastJsonHttpMessageConverter 添加到列表末尾
|
||||
converters.add(fastJsonConverter);
|
||||
}
|
||||
|
||||
private FastJsonConfig getFastJsonConfig() {
|
||||
FastJsonConfig config = new FastJsonConfig();
|
||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// 开启引用检测, 枚举支持
|
||||
@@ -104,11 +113,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
|
||||
// config.setWriterFeatures(JSONWriter.Feature.WriteLongAsString);
|
||||
config.setWriterFeatures(JSONWriter.Feature.WriteEnumUsingToString);
|
||||
config.setWriterFeatures(JSONWriter.Feature.ReferenceDetection);
|
||||
fastJsonConverter.setFastJsonConfig(config);
|
||||
fastJsonConverter.setSupportedMediaTypes(supportMediaTypeList);
|
||||
fastJsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
|
||||
// 将 FastJsonHttpMessageConverter 添加到列表末尾
|
||||
converters.add(fastJsonConverter);
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ public class BizLockAspect {
|
||||
@Around("@annotation(bizLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, BizLock bizLock) throws Throwable {
|
||||
// 解析SpEL表达式生成最终的lockKey
|
||||
String lockKey = parseLockKey(joinPoint, bizLock);
|
||||
String lockKey = this.parseLockKey(joinPoint, bizLock);
|
||||
boolean success = redisHelper.setIfAbsent(lockKey, lockKey, bizLock.leaseTime(), bizLock.timeUnit());
|
||||
if (!success) {
|
||||
throw new BadRequestException(bizLock.errorMessage());
|
||||
|
||||
+2
-10
@@ -32,11 +32,7 @@ public class MpMetaObjectHandler implements MetaObjectHandler {
|
||||
this.strictInsertFill(metaObject, "createTime", Date.class, now);
|
||||
this.strictInsertFill(metaObject, "updateTime", Date.class, now);
|
||||
/* 操作人 */
|
||||
String username = "System";
|
||||
try {
|
||||
username = KitSecurityHelper.getCurrentUsername();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
String username = KitSecurityHelper.safeGetUsername();
|
||||
this.strictInsertFill(metaObject, "createBy", String.class, username);
|
||||
this.strictInsertFill(metaObject, "updateBy", String.class, username);
|
||||
}
|
||||
@@ -46,11 +42,7 @@ public class MpMetaObjectHandler implements MetaObjectHandler {
|
||||
/* 更新时间 */
|
||||
this.strictUpdateFill(metaObject, "updateTime", Date.class, DateTime.now());
|
||||
/* 操作人 */
|
||||
String username = "System";
|
||||
try {
|
||||
username = KitSecurityHelper.getCurrentUsername();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
String username = KitSecurityHelper.safeGetUsername();
|
||||
this.strictUpdateFill(metaObject, "updateBy", String.class, username);
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -26,8 +26,10 @@ import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
@@ -106,6 +108,19 @@ public class KitSecurityHelper {
|
||||
return getCurrentUsername(getToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统用户名称
|
||||
*
|
||||
* @return 系统用户名称
|
||||
*/
|
||||
public static String safeGetUsername() {
|
||||
try {
|
||||
return getCurrentUsername(getToken());
|
||||
} catch (Exception e) {
|
||||
return "anonymous";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统用户名称
|
||||
*
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ public class TokenFilter extends GenericFilterBean {
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
|
||||
String token = resolveToken(httpServletRequest);
|
||||
String token = this.resolveToken(httpServletRequest);
|
||||
// 对于 Token 为空的不需要去查 Redis
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
// 获取用户Token的Key
|
||||
|
||||
+5
-5
@@ -105,8 +105,8 @@ public class TokenProvider implements InitializingBean {
|
||||
* @param token /
|
||||
* @return /
|
||||
*/
|
||||
Authentication getAuthentication(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
protected Authentication getAuthentication(String token) {
|
||||
Claims claims = this.getClaims(token);
|
||||
User principal = new User(claims.getSubject(), "******", new ArrayList<>());
|
||||
return new UsernamePasswordAuthenticationToken(principal, token, new ArrayList<>());
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public class TokenProvider implements InitializingBean {
|
||||
*/
|
||||
public void checkRenewal(String token) {
|
||||
// 判断是否续期token,计算token的过期时间
|
||||
String loginKey = loginKey(token);
|
||||
String loginKey = this.loginKey(token);
|
||||
long time = redisHelper.getExpire(loginKey) * 1000;
|
||||
Date expireDate = DateUtil.offset(new Date(), DateField.MILLISECOND, (int) time);
|
||||
// 判断当前时间与过期时间的时间差
|
||||
@@ -150,7 +150,7 @@ public class TokenProvider implements InitializingBean {
|
||||
* @return key
|
||||
*/
|
||||
public String loginKey(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
Claims claims = this.getClaims(token);
|
||||
return SystemCacheKey.ONLINE_USER + claims.getSubject() + ":" + getId(token);
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ public class TokenProvider implements InitializingBean {
|
||||
* @return /
|
||||
*/
|
||||
public String getId(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
Claims claims = this.getClaims(token);
|
||||
return claims.get(AUTHORITIES_UUID_KEY, String.class);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-5
@@ -87,7 +87,8 @@ public class QuartzManage {
|
||||
*/
|
||||
public void updateJobCron(SystemQuartzJobTb quartzJob) {
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
|
||||
String uuid = JOB_NAME + quartzJob.getId();
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(uuid);
|
||||
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||
// 如果不存在则创建一个定时任务
|
||||
if (trigger == null) {
|
||||
@@ -117,7 +118,8 @@ public class QuartzManage {
|
||||
*/
|
||||
public void deleteJob(SystemQuartzJobTb quartzJob) {
|
||||
try {
|
||||
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
|
||||
String uuid = JOB_NAME + quartzJob.getId();
|
||||
JobKey jobKey = JobKey.jobKey(uuid);
|
||||
scheduler.pauseJob(jobKey);
|
||||
scheduler.deleteJob(jobKey);
|
||||
} catch (Exception e) {
|
||||
@@ -133,13 +135,14 @@ public class QuartzManage {
|
||||
*/
|
||||
public void resumeJob(SystemQuartzJobTb quartzJob) {
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
|
||||
String uuid = JOB_NAME + quartzJob.getId();
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(uuid);
|
||||
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||
// 如果不存在则创建一个定时任务
|
||||
if (trigger == null) {
|
||||
addJob(quartzJob);
|
||||
}
|
||||
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
|
||||
JobKey jobKey = JobKey.jobKey(uuid);
|
||||
scheduler.resumeJob(jobKey);
|
||||
} catch (Exception e) {
|
||||
log.error("恢复定时任务失败", e);
|
||||
@@ -178,7 +181,8 @@ public class QuartzManage {
|
||||
*/
|
||||
public void pauseJob(SystemQuartzJobTb quartzJob) {
|
||||
try {
|
||||
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
|
||||
String uuid = JOB_NAME + quartzJob.getId();
|
||||
JobKey jobKey = JobKey.jobKey(uuid);
|
||||
scheduler.pauseJob(jobKey);
|
||||
} catch (Exception e) {
|
||||
log.error("定时任务暂停失败", e);
|
||||
|
||||
+1
-1
@@ -573,7 +573,7 @@ public class SystemMenuService {
|
||||
menuSet.add(menu);
|
||||
List<SystemMenuTb> menus = this.listMenuByPid(menu.getId(), true);
|
||||
if (CollUtil.isNotEmpty(menus)) {
|
||||
queryChildMenuByArgs(menus, menuSet);
|
||||
this.queryChildMenuByArgs(menus, menuSet);
|
||||
}
|
||||
}
|
||||
return menuSet;
|
||||
|
||||
+16
-9
@@ -37,6 +37,7 @@ import cn.odboy.util.KitValidUtil;
|
||||
import cn.odboy.util.xlsx.KitExcelExporter;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.quartz.CronExpression;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -237,20 +238,26 @@ public class SystemQuartzJobService {
|
||||
List<SystemQuartzJobTb> systemQuartzJobTbs = this.queryQuartzJobByArgs(args);
|
||||
List<SystemQuartzJobExportRowVo> rowVos = new ArrayList<>();
|
||||
for (SystemQuartzJobTb dataObject : systemQuartzJobTbs) {
|
||||
SystemQuartzJobExportRowVo rowVo = new SystemQuartzJobExportRowVo();
|
||||
rowVo.setJobName(dataObject.getJobName());
|
||||
rowVo.setBeanName(dataObject.getBeanName());
|
||||
rowVo.setMethodName(dataObject.getMethodName());
|
||||
rowVo.setParams(dataObject.getParams());
|
||||
rowVo.setCronExpression(dataObject.getCronExpression());
|
||||
rowVo.setIsPause(dataObject.getIsPause() ? "暂停中" : "运行中");
|
||||
rowVo.setDescription(dataObject.getDescription());
|
||||
rowVo.setCreateTime(dataObject.getCreateTime());
|
||||
SystemQuartzJobExportRowVo rowVo = this.getExportRowVo(dataObject);
|
||||
rowVos.add(rowVo);
|
||||
}
|
||||
KitExcelExporter.exportSimple(response, "定时任务数据", SystemQuartzJobExportRowVo.class, rowVos);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SystemQuartzJobExportRowVo getExportRowVo(SystemQuartzJobTb dataObject) {
|
||||
SystemQuartzJobExportRowVo rowVo = new SystemQuartzJobExportRowVo();
|
||||
rowVo.setJobName(dataObject.getJobName());
|
||||
rowVo.setBeanName(dataObject.getBeanName());
|
||||
rowVo.setMethodName(dataObject.getMethodName());
|
||||
rowVo.setParams(dataObject.getParams());
|
||||
rowVo.setCronExpression(dataObject.getCronExpression());
|
||||
rowVo.setIsPause(dataObject.getIsPause() ? "暂停中" : "运行中");
|
||||
rowVo.setDescription(dataObject.getDescription());
|
||||
rowVo.setCreateTime(dataObject.getCreateTime());
|
||||
return rowVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出定时任务日志数据
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user