mirror of https://gitee.com/stylefeng/roses
修改配置文件初始化问题
parent
465f54f8db
commit
2af50ade4e
|
@ -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.
|
||||
*
|
||||
* Guns采用APACHE 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();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue