初始化用户
All checks were successful
local-test分支构建docker并发布运行 / Docker-Build (push) Successful in 1m18s

This commit is contained in:
tiger_zhou 2024-11-20 11:46:36 +08:00
parent e93948bdd1
commit 953294ea31
5 changed files with 125 additions and 3 deletions

View File

@ -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";

View File

@ -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<String, AuthRole> staticInitialData() {
Map<String, AuthRole> 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<User> defulatUser() {
List<User> 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<User> defulatUsers = this.defulatUser();
List<User> 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()));
}
}
}
}

View File

@ -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);

View File

@ -59,6 +59,7 @@ occ-not-handle:
receive-msg-timeout: 1
#检测硬件变化(cpu,内存)
monitor-handware-change: true
password-sult: "4a6d74126bfd06d69406fcccb7e7d5d9"
#management:
# endpoints:
# web:

View File

@ -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;
}
}