From 39ad8a094495f2524df2d7b0bcce494b1fc86e7f Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Sun, 14 Apr 2024 19:25:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=89=E5=85=A8=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...slOrganizeInternalOaRunnerApplication.java | 59 ---------- .../jsl/oa/config/startup/PrepareData.java | 35 ++++++ .../config/startup/StartupConfiguration.java | 103 ++++++++++++++++++ 3 files changed, 138 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/com/jsl/oa/JslOrganizeInternalOaRunnerApplication.java create mode 100644 src/main/java/com/jsl/oa/config/startup/PrepareData.java create mode 100644 src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java diff --git a/src/main/java/com/jsl/oa/JslOrganizeInternalOaRunnerApplication.java b/src/main/java/com/jsl/oa/JslOrganizeInternalOaRunnerApplication.java deleted file mode 100644 index 6ccf80f..0000000 --- a/src/main/java/com/jsl/oa/JslOrganizeInternalOaRunnerApplication.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.jsl.oa; - -import com.google.gson.Gson; -import com.jsl.oa.common.constant.SafeConstants; -import com.jsl.oa.mapper.InfoMapper; -import com.jsl.oa.model.dodata.ConfigDO; -import com.jsl.oa.model.vodata.business.InfoAboutSecurityKey; -import com.jsl.oa.utils.Processing; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.SmartInitializingSingleton; -import org.springframework.stereotype.Component; - -import java.sql.Timestamp; - -/** - *

启动类

- *
- * 用于启动项目 - * - * @version v1.1.0 - * @see org.springframework.boot.SpringApplication - * @see org.springframework.boot.autoconfigure.SpringBootApplication - * @since v1.1.0 - * @author xiaofeng - */ -@Component -@RequiredArgsConstructor -public class JslOrganizeInternalOaRunnerApplication implements SmartInitializingSingleton { - private final Gson gson = new Gson(); - private final InfoMapper infoMapper; - - - /** - *

获取安全密钥

- *
- * 从数据库中获取安全密钥 - */ - @Override - public void afterSingletonsInstantiated() { - // 获取数据库中的 SecurityKey - try { - SafeConstants.setSecretKey(infoMapper.getSecurityKey().getData()); - } catch (NullPointerException exception) { - // 生成密钥 - String key = Processing.generateKey(System.currentTimeMillis()); - InfoAboutSecurityKey infoAboutSecurityKey = new InfoAboutSecurityKey(); - infoAboutSecurityKey.setKey(key) - .setUpdateTime(System.currentTimeMillis()); - String json = gson.toJson(infoAboutSecurityKey, InfoAboutSecurityKey.class); - // 更新密钥 - ConfigDO configDO = new ConfigDO(); - configDO.setValue("security_key") - .setData(json) - .setCreatedAt(new Timestamp(System.currentTimeMillis())); - infoMapper.insertSecurityKey(configDO); - SafeConstants.setSecretKey(key); - } - } -} diff --git a/src/main/java/com/jsl/oa/config/startup/PrepareData.java b/src/main/java/com/jsl/oa/config/startup/PrepareData.java new file mode 100644 index 0000000..f9fafa6 --- /dev/null +++ b/src/main/java/com/jsl/oa/config/startup/PrepareData.java @@ -0,0 +1,35 @@ +package com.jsl.oa.config.startup; + +import com.jsl.oa.model.dodata.RoleDO; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.jdbc.core.JdbcTemplate; + +@Slf4j +@RequiredArgsConstructor +public class PrepareData { + private final JdbcTemplate jdbcTemplate; + + /** + * 检查角色 + *
+ * 检查检查指定的角色是否存在,如果不存在则创建 + */ + public void checkRole(String roleName, String displayName) { + RoleDO getRole = jdbcTemplate.queryForObject( + "SELECT * FROM organize_oa.oa_role WHERE role_name = ?", + RoleDO.class, + roleName + ); + // 检查角色是否存在 + if (getRole == null) { + // 创建角色 + log.debug("[Preparation] 创建角色 [{}]{}", roleName, displayName); + jdbcTemplate.update( + "INSERT INTO organize_oa.oa_role (role_name, display_name) VALUES (?,?)", + roleName, + displayName + ); + } + } +} diff --git a/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java b/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java new file mode 100644 index 0000000..a3202a3 --- /dev/null +++ b/src/main/java/com/jsl/oa/config/startup/StartupConfiguration.java @@ -0,0 +1,103 @@ +package com.jsl.oa.config.startup; + +import com.google.gson.Gson; +import com.jsl.oa.common.constant.SafeConstants; +import com.jsl.oa.model.dodata.ConfigDO; +import com.jsl.oa.model.vodata.business.InfoAboutSecurityKey; +import com.jsl.oa.utils.Processing; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.Timestamp; + +/** + * 系统启动时进行的一些初始化操作 + *
+ * 1. 检查数据库完整性 + * 2. 检查系统配置 + * 3. 检查系统权限 + * 4. 检查系统数据 + * + * @author xiao_lfeng + * @version v1.2.0 + * @since v1.2.0 + */ +@Slf4j +@Configuration +@RequiredArgsConstructor +public class StartupConfiguration { + private final JdbcTemplate jdbcTemplate; + private final PrepareData prepareData = new PrepareData(jdbcTemplate); + + @Bean + @Order(1) + public CommandLineRunner startUpPreparation() { + return args -> { + log.info("============================================================"); + log.info("[Preparation] 系统进行准备检查"); + }; + } + + /** + * 对数据库进行完整性检查 + *
+ * 对数据库进行完整性检查,检查数据库是否有数据缺失等信息 + */ + @Bean + @Order(2) + public CommandLineRunner sqlDataPreparation() { + return args -> { + log.info("[Preparation] 系统进行数据库完整性检查"); + // 检查角色信息是否完整 + prepareData.checkRole("console", "超级管理员"); + prepareData.checkRole("principal", "负责人"); + prepareData.checkRole("developer", "开发者"); + }; + } + + /** + * 准备安全密钥 + *
+ * 准备安全密钥,用于加密解密等操作 + */ + @Bean + @Order(3) + public CommandLineRunner prepareKey() { + return args -> { + log.info("[Preparation] 系统进行安全密钥准备"); + Gson gson = new Gson(); + // 获取数据库中的安全密钥 + ConfigDO getSecurityKey = jdbcTemplate.queryForObject( + "SELECT * FROM organize_oa.oa_config WHERE value = ?", + ConfigDO.class, + "security_key" + ); + if (getSecurityKey != null) { + SafeConstants.setSecretKey(getSecurityKey.getData()); + } else { + // 生成密钥 + String key = Processing.generateKey(System.currentTimeMillis()); + InfoAboutSecurityKey infoAboutSecurityKey = new InfoAboutSecurityKey(); + infoAboutSecurityKey.setKey(key) + .setUpdateTime(System.currentTimeMillis()); + String json = gson.toJson(infoAboutSecurityKey, InfoAboutSecurityKey.class); + // 更新密钥 + ConfigDO configDO = new ConfigDO(); + configDO.setValue("security_key") + .setData(json) + .setCreatedAt(new Timestamp(System.currentTimeMillis())); + // 初始化密钥 + jdbcTemplate.update("INSERT INTO organize_oa.oa_config (value, data) VALUES (?, ?)", + configDO.getValue(), + configDO.getData() + ); + SafeConstants.setSecretKey(key); + } + }; + } +}