修改配置文件初始化问题

pull/22/head
rays 2021-07-17 18:09:17 +08:00
parent 465f54f8db
commit 2af50ade4e
5 changed files with 119 additions and 22 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.config.api;
/**
*
*
* @author majianguo
* @date 2021/7/17 11:52
*/
public interface ConfigInitCallbackApi {
/**
*
* <p>
*
*
* @author majianguo
* @date 2021/7/17 11:54
**/
void initBefore();
/**
*
* <p>
* ,
*
* @author majianguo
* @date 2021/7/17 11:55
**/
void initAfter();
}

View File

@ -28,6 +28,8 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigInitCallbackApi;
import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
@ -50,7 +52,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -63,9 +65,6 @@ import java.util.Map;
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
@Resource
private ConfigInitStrategyApi configInitStrategyApi;
@Override
@Transactional(rollbackFor = Exception.class)
public void add(SysConfigParam sysConfigParam) {
@ -161,6 +160,16 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
throw new ConfigException(ConfigExceptionEnum.CONFIG_INIT_ALREADY);
}
// 获取初始化回调接口的所有实现类
Map<String, ConfigInitCallbackApi> beans = SpringUtil.getBeansOfType(ConfigInitCallbackApi.class);
// 调用初始化之前回调
if (ObjectUtil.isNotNull(beans)) {
for (ConfigInitCallbackApi initCallbackApi : beans.values()) {
initCallbackApi.initBefore();
}
}
// 添加系统已经初始化的配置
Map<String, String> sysConfigs = configInitRequest.getSysConfigs();
sysConfigs.put(RuleConstants.SYSTEM_CONFIG_INIT_FLAG_NAME, "true");
@ -183,6 +192,13 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
// 更新缓存
ConfigContext.me().putConfig(configCode, configValue);
}
// 调用初始化之后回调
if (ObjectUtil.isNotNull(beans)) {
for (ConfigInitCallbackApi initCallbackApi : beans.values()) {
initCallbackApi.initAfter();
}
}
}
@Override
@ -206,7 +222,12 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
@Override
public List<ConfigInitItem> getInitConfigs() {
return configInitStrategyApi.getInitConfigs();
List<ConfigInitItem> configInitItemList = new ArrayList<>();
Map<String, ConfigInitStrategyApi> beans = SpringUtil.getBeansOfType(ConfigInitStrategyApi.class);
for (ConfigInitStrategyApi value : beans.values()) {
configInitItemList.addAll(value.getInitConfigs());
}
return configInitItemList;
}
/**

View File

@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi;
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitItem;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
@ -15,6 +16,7 @@ import java.util.List;
* @author fengshuonan
* @date 2021/7/8 17:47
*/
@Component
public class DefaultStrategyImpl implements ConfigInitStrategyApi {
@Override
@ -37,7 +39,7 @@ public class DefaultStrategyImpl implements ConfigInitStrategyApi {
configInitItems.add(new ConfigInitItem("系统默认密码", "SYS_DEFAULT_PASSWORD", "123456", "用在重置密码的默认密码"));
configInitItems.add(new ConfigInitItem("系统发布版本", "SYS_RELEASE_VERSION", DateUtil.format(new Date(), "yyyyMMdd"), "系统发布的版本号"));
configInitItems.add(new ConfigInitItem("是否开启demo演示", "SYS_DEMO_ENV_FLAG", "false", "是否开启demo演示环境"));
configInitItems.add(new ConfigInitItem("AES秘钥", "SYS_ENCRYPT_SECRET_KEY", RandomUtil.randomString(20), "对称加密秘钥,用在数据库数据加密"));
configInitItems.add(new ConfigInitItem("数据库加密AES秘钥", "SYS_ENCRYPT_SECRET_KEY", RandomUtil.randomString(32), "对称加密秘钥,用在数据库数据加密"));
return configInitItems;
}

View File

@ -24,10 +24,6 @@
*/
package cn.stylefeng.roses.kernel.config.starter;
import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi;
import cn.stylefeng.roses.kernel.config.modular.strategy.DefaultStrategyImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
@ -39,16 +35,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class GunsSysConfigAutoConfiguration {
/**
* api
*
* @author fengshuonan
* @date 2021/7/8 17:48
*/
@Bean
@ConditionalOnMissingBean(ConfigInitStrategyApi.class)
public ConfigInitStrategyApi configInitStrategyApi() {
return new DefaultStrategyImpl();
}
}

View File

@ -0,0 +1,36 @@
package cn.stylefeng.roses.kernel.security.database.init;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigInitCallbackApi;
import cn.stylefeng.roses.kernel.security.api.expander.SecurityConfigExpander;
import cn.stylefeng.roses.kernel.security.database.algorithm.EncryptAlgorithmApi;
import org.springframework.stereotype.Component;
import javax.security.auth.login.LoginContext;
import java.nio.charset.StandardCharsets;
/**
*
*
* @author majianguo
* @date 2021/7/17 16:01
*/
@Component
public class DefaultConfigInitCallbackImpl implements ConfigInitCallbackApi {
@Override
public void initBefore() {
}
@Override
public void initAfter() {
// 修改数据库秘钥AES实例
EncryptAlgorithmApi encryptAlgorithmApi = SpringUtil.getBean(EncryptAlgorithmApi.class);
SymmetricCrypto symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.AES, SecurityConfigExpander.getEncryptSecretKey().getBytes(StandardCharsets.UTF_8));
ReflectUtil.setFieldValue(encryptAlgorithmApi, "symmetricCrypto", symmetricCrypto);
}
}