mirror of https://gitee.com/stylefeng/roses
修改配置文件初始化问题
parent
ccd16bacd2
commit
8f4045285a
|
@ -1,5 +1,7 @@
|
|||
package cn.stylefeng.roses.kernel.security.database.algorithm;
|
||||
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||
|
||||
/**
|
||||
* 加密算法接口
|
||||
* <p>
|
||||
|
@ -29,4 +31,14 @@ public interface EncryptAlgorithmApi {
|
|||
* @date 2021/7/3 11:33
|
||||
**/
|
||||
String decrypt(String cipher);
|
||||
|
||||
/**
|
||||
* 设置加密实例
|
||||
* <p>
|
||||
* 只有在项目首次初始化配置时调用,随意调用会导致之前加密数据无法解密
|
||||
*
|
||||
* @author majianguo
|
||||
* @date 2021/7/19 10:04
|
||||
**/
|
||||
void setInstance(SymmetricCrypto instance);
|
||||
}
|
||||
|
|
|
@ -15,12 +15,17 @@ public class AesEncryptAlgorithmApiImpl implements EncryptAlgorithmApi {
|
|||
/**
|
||||
* AES加密实体类
|
||||
*/
|
||||
public final SymmetricCrypto symmetricCrypto;
|
||||
public SymmetricCrypto symmetricCrypto;
|
||||
|
||||
public AesEncryptAlgorithmApiImpl(byte[] key) {
|
||||
symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInstance(SymmetricCrypto instance) {
|
||||
this.symmetricCrypto = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encrypt(String encryptedData) {
|
||||
return symmetricCrypto.encryptHex(encryptedData);
|
||||
|
@ -30,5 +35,4 @@ public class AesEncryptAlgorithmApiImpl implements EncryptAlgorithmApi {
|
|||
public String decrypt(String cipher) {
|
||||
return symmetricCrypto.decryptStr(cipher);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
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.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.security.auth.login.LoginContext;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +19,9 @@ import java.nio.charset.StandardCharsets;
|
|||
@Component
|
||||
public class DefaultConfigInitCallbackImpl implements ConfigInitCallbackApi {
|
||||
|
||||
@Autowired
|
||||
private EncryptAlgorithmApi encryptAlgorithmApi;
|
||||
|
||||
@Override
|
||||
public void initBefore() {
|
||||
|
||||
|
@ -29,8 +30,7 @@ public class DefaultConfigInitCallbackImpl implements ConfigInitCallbackApi {
|
|||
@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);
|
||||
encryptAlgorithmApi.setInstance(symmetricCrypto);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue