feat: 安全相关的准备
This commit is contained in:
parent
fd22ba9dc6
commit
39ad8a0944
@ -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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <h1>启动类</h1>
|
|
||||||
* <hr/>
|
|
||||||
* 用于启动项目
|
|
||||||
*
|
|
||||||
* @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;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <h1>获取安全密钥</h1>
|
|
||||||
* <hr/>
|
|
||||||
* 从数据库中获取安全密钥
|
|
||||||
*/
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
35
src/main/java/com/jsl/oa/config/startup/PrepareData.java
Normal file
35
src/main/java/com/jsl/oa/config/startup/PrepareData.java
Normal file
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查角色
|
||||||
|
* <hr/>
|
||||||
|
* 检查检查指定的角色是否存在,如果不存在则创建
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统启动时进行的一些初始化操作
|
||||||
|
* <hr/>
|
||||||
|
* 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] 系统进行准备检查");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对数据库进行完整性检查
|
||||||
|
* <hr/>
|
||||||
|
* 对数据库进行完整性检查,检查数据库是否有数据缺失等信息
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Order(2)
|
||||||
|
public CommandLineRunner sqlDataPreparation() {
|
||||||
|
return args -> {
|
||||||
|
log.info("[Preparation] 系统进行数据库完整性检查");
|
||||||
|
// 检查角色信息是否完整
|
||||||
|
prepareData.checkRole("console", "超级管理员");
|
||||||
|
prepareData.checkRole("principal", "负责人");
|
||||||
|
prepareData.checkRole("developer", "开发者");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 准备安全密钥
|
||||||
|
* <hr/>
|
||||||
|
* 准备安全密钥,用于加密解密等操作
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user