From 953294ea3190aed874a83cca49b2130d9ec9f903 Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Wed, 20 Nov 2024 11:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../club/joylink/xiannccda/entity/User.java | 6 +- .../club/joylink/xiannccda/task/InitTask.java | 86 ++++++++++++++++++- .../xiannccda/ws/LineDeviceMessageServer.java | 2 + src/main/resources/application.yml | 1 + .../joylink/xiannccda/util/impo/TestObj.java | 33 +++++++ 5 files changed, 125 insertions(+), 3 deletions(-) diff --git a/src/main/java/club/joylink/xiannccda/entity/User.java b/src/main/java/club/joylink/xiannccda/entity/User.java index e599cbc..02990af 100644 --- a/src/main/java/club/joylink/xiannccda/entity/User.java +++ b/src/main/java/club/joylink/xiannccda/entity/User.java @@ -3,6 +3,7 @@ package club.joylink.xiannccda.entity; import club.joylink.xiannccda.constants.common.LoginUserAttr.DefaultUserRuleEnum; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import jakarta.validation.constraints.NotBlank; import java.time.LocalDateTime; @@ -26,7 +27,7 @@ import lombok.experimental.Accessors; public class User { @TableId(value = "id", type = IdType.AUTO) - private Integer id; + private Long id; @Schema(description = "名字") @NotBlank(message = "名字不能为空", groups = {Register.class}) @@ -46,6 +47,9 @@ public class User { @Schema(description = "用户默认角色") private DefaultUserRuleEnum defaultRole; + + @TableField(exist = false) + private Long defulatRuleId; public static final String ID = "id"; public static final String NAME = "name"; diff --git a/src/main/java/club/joylink/xiannccda/task/InitTask.java b/src/main/java/club/joylink/xiannccda/task/InitTask.java index f818b54..d182e65 100644 --- a/src/main/java/club/joylink/xiannccda/task/InitTask.java +++ b/src/main/java/club/joylink/xiannccda/task/InitTask.java @@ -1,17 +1,27 @@ package club.joylink.xiannccda.task; import club.joylink.xiannccda.constants.common.LineTypeEnum; +import club.joylink.xiannccda.constants.common.LoginUserAttr.DefaultUserRuleEnum; import club.joylink.xiannccda.entity.AuthRole; import club.joylink.xiannccda.entity.AuthRole.RoleConfig; +import club.joylink.xiannccda.entity.AuthRoleUserRef; +import club.joylink.xiannccda.entity.User; import club.joylink.xiannccda.repository.IAuthRoleRepository; +import club.joylink.xiannccda.repository.IAuthRoleUserRefRepository; +import club.joylink.xiannccda.repository.IUserRepository; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.google.common.collect.Lists; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @@ -21,13 +31,21 @@ public class InitTask implements ApplicationRunner { @Autowired private IAuthRoleRepository authRoleRepository; + @Autowired + private IUserRepository userRepository; + + @Autowired + private IAuthRoleUserRefRepository authRoleUserRefRepository; + + @Value("${password-sult}") + private String passwordSult; private Map staticInitialData() { Map roleMap = new HashMap<>(); - AuthRole r1 = new AuthRole(1L, "超级管理员"); + AuthRole r1 = new AuthRole(1L, "NCC-ATS"); r1.setRoleConfig(List.of(initRoleConfig(3, LineTypeEnum.NCC), initRoleConfig(4, LineTypeEnum.NCC))); - roleMap.put("超级管理员", r1); + roleMap.put("NCC-ATS", r1); r1 = new AuthRole(2L, "3号线ATS"); r1.setRoleConfig(List.of(initRoleConfig(3, LineTypeEnum.OCC))); roleMap.put("3号线ATS", r1); @@ -37,6 +55,56 @@ public class InitTask implements ApplicationRunner { return roleMap; } + private List defulatUser() { + List users = Lists.newArrayList(); + String adminPW = md5(String.format("%s%s", "jl0503", this.passwordSult)); + String nccAndOccPW = md5(String.format("%s%s", "999999", this.passwordSult)); + User adminUser = new User(); + + adminUser.setId(1L).setName("jladmin").setMobile("jladmin").setPassword(adminPW) + .setRegisterTime(LocalDateTime.now()).setDefaultRole(DefaultUserRuleEnum.ADMIN).setDefulatRuleId(1L); + + User nccUser = new User(); + nccUser.setId(2L).setName("ncc").setMobile("ncc").setPassword(nccAndOccPW) + .setRegisterTime(LocalDateTime.now()).setDefaultRole(DefaultUserRuleEnum.USER) + .setDefulatRuleId(1L); + + User occ3 = new User(); + occ3.setId(3L).setName("occ-3").setMobile("occ-3").setPassword(nccAndOccPW) + .setRegisterTime(LocalDateTime.now()).setDefaultRole(DefaultUserRuleEnum.USER) + .setDefulatRuleId(2L); + + User occ4 = new User(); + occ4.setId(4L).setName("occ-4").setMobile("occ-4").setPassword(nccAndOccPW) + .setRegisterTime(LocalDateTime.now()).setDefaultRole(DefaultUserRuleEnum.USER) + .setDefulatRuleId(3L); + + users.add(adminUser); + users.add(nccUser); + users.add(occ3); + users.add(occ4); + return users; + } + + public static String md5(String plainString) { + String cipherString = null; + try { + // 获取实例 + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + // 计算摘要 + byte[] cipherBytes = messageDigest.digest(plainString.getBytes(StandardCharsets.UTF_8)); + // 输出为16进制字符串 + StringBuilder sb = new StringBuilder(); + for (byte b : cipherBytes) { + sb.append(String.format("%02x", b)); + } + cipherString = sb.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return cipherString; + } + private RoleConfig initRoleConfig(int lineId, LineTypeEnum lineType) { return new RoleConfig(lineId, lineType); @@ -53,7 +121,21 @@ public class InitTask implements ApplicationRunner { if (authRoles.stream().filter(d -> StringUtils.equals(d.getName(), k)).findFirst().isEmpty()) { v.setRoleConfigStr(JSON.toJSONString(v.tmpRoleConfig())); this.authRoleRepository.saveOrUpdate(v); + } }); + initUser(); + } + + private void initUser() { + List defulatUsers = this.defulatUser(); + List findUsers = this.userRepository.lambdaQuery().in(User::getMobile, defulatUsers.stream().map(User::getMobile).toList()).list(); + for (User du : defulatUsers) { + if (findUsers.stream().filter(d -> StringUtils.equals(du.getMobile(), d.getMobile())).findFirst().isEmpty()) { + this.userRepository.save(du); + + this.authRoleUserRefRepository.save(new AuthRoleUserRef(du.getDefulatRuleId(), du.getId())); + } + } } } diff --git a/src/main/java/club/joylink/xiannccda/ws/LineDeviceMessageServer.java b/src/main/java/club/joylink/xiannccda/ws/LineDeviceMessageServer.java index 9e59e17..87033d9 100644 --- a/src/main/java/club/joylink/xiannccda/ws/LineDeviceMessageServer.java +++ b/src/main/java/club/joylink/xiannccda/ws/LineDeviceMessageServer.java @@ -140,6 +140,8 @@ public class LineDeviceMessageServer implements IMessageServer { if (this.showTimeCounter(timeHour)) { if (pfb.getBizWaitTimerGoing()) { pfb.setBizWaitTimeSec(System.currentTimeMillis() / 1000 - pfb.getBizWaitStartTimeSec()); + } else { + pfb.setBizWaitTimeSec(0); } } else { pfb.setBizWaitTimeSec(0); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a9b9e0c..d4ac047 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -59,6 +59,7 @@ occ-not-handle: receive-msg-timeout: 1 #检测硬件变化(cpu,内存) monitor-handware-change: true +password-sult: "4a6d74126bfd06d69406fcccb7e7d5d9" #management: # endpoints: # web: diff --git a/src/test/java/club/joylink/xiannccda/util/impo/TestObj.java b/src/test/java/club/joylink/xiannccda/util/impo/TestObj.java index 752e776..ee085ee 100644 --- a/src/test/java/club/joylink/xiannccda/util/impo/TestObj.java +++ b/src/test/java/club/joylink/xiannccda/util/impo/TestObj.java @@ -2,6 +2,11 @@ package club.joylink.xiannccda.util.impo; import club.joylink.xiannccda.constants.common.LineTypeEnum; import club.joylink.xiannccda.entity.AlertTip; +import cn.hutool.core.util.HashUtil; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import org.junit.jupiter.api.Test; +import org.springframework.util.DigestUtils; public class TestObj { @@ -22,4 +27,32 @@ public class TestObj { return msg; } + @Test + public void createPasswordTest() { + String password = "123456" + "4a6d74126bfd06d69406fcccb7e7d5d9"; + String s = md5(password); + System.out.println(s); +// 0a81329109296bcc6c5fce4e2f68eb9b +// 202cb962ac59075b964b07152d234b70 + } + + public static String md5(String plainString) { + String cipherString = null; + try { + // 获取实例 + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + // 计算摘要 + byte[] cipherBytes = messageDigest.digest(plainString.getBytes(StandardCharsets.UTF_8)); + // 输出为16进制字符串 + StringBuilder sb = new StringBuilder(); + for (byte b : cipherBytes) { + sb.append(String.format("%02x", b)); + } + cipherString = sb.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return cipherString; + } + }