mirror of https://gitee.com/stylefeng/roses
数据库字段加密,删除无用操作
parent
be1e279f35
commit
9daebb9456
|
@ -31,14 +31,4 @@ 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);
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package cn.stylefeng.roses.kernel.security.database.algorithm.impl;
|
|||
|
||||
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||
import cn.stylefeng.roses.kernel.security.api.expander.SecurityConfigExpander;
|
||||
import cn.stylefeng.roses.kernel.security.database.algorithm.EncryptAlgorithmApi;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* AES 加密解密实现
|
||||
*
|
||||
|
@ -12,27 +15,15 @@ import cn.stylefeng.roses.kernel.security.database.algorithm.EncryptAlgorithmApi
|
|||
*/
|
||||
public class AesEncryptAlgorithmApiImpl implements EncryptAlgorithmApi {
|
||||
|
||||
/**
|
||||
* AES加密实体类
|
||||
*/
|
||||
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) {
|
||||
SymmetricCrypto symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.AES, SecurityConfigExpander.getEncryptSecretKey().getBytes(StandardCharsets.UTF_8));
|
||||
return symmetricCrypto.encryptHex(encryptedData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decrypt(String cipher) {
|
||||
SymmetricCrypto symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.AES, SecurityConfigExpander.getEncryptSecretKey().getBytes(StandardCharsets.UTF_8));
|
||||
return symmetricCrypto.decryptStr(cipher);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package cn.stylefeng.roses.kernel.security.database.init;
|
||||
|
||||
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||
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 java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 数据库加密配置初始化回调
|
||||
*
|
||||
* @author majianguo
|
||||
* @date 2021/7/17 16:01
|
||||
*/
|
||||
@Component
|
||||
public class DefaultConfigInitCallbackImpl implements ConfigInitCallbackApi {
|
||||
|
||||
@Autowired
|
||||
private EncryptAlgorithmApi encryptAlgorithmApi;
|
||||
|
||||
@Override
|
||||
public void initBefore() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initAfter() {
|
||||
// 修改数据库秘钥AES实例
|
||||
SymmetricCrypto symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.AES, SecurityConfigExpander.getEncryptSecretKey().getBytes(StandardCharsets.UTF_8));
|
||||
encryptAlgorithmApi.setInstance(symmetricCrypto);
|
||||
}
|
||||
}
|
|
@ -24,15 +24,12 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.security.starter;
|
||||
|
||||
import cn.stylefeng.roses.kernel.security.api.expander.SecurityConfigExpander;
|
||||
import cn.stylefeng.roses.kernel.security.database.algorithm.EncryptAlgorithmApi;
|
||||
import cn.stylefeng.roses.kernel.security.database.algorithm.impl.AesEncryptAlgorithmApiImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 加密算法自动配置
|
||||
*
|
||||
|
@ -52,8 +49,7 @@ public class EncryptAlgorithmAutoConfiguration {
|
|||
@Bean
|
||||
@ConditionalOnMissingBean(EncryptAlgorithmApi.class)
|
||||
public EncryptAlgorithmApi encryptAlgorithmApi() {
|
||||
String encryptSecretKey = SecurityConfigExpander.getEncryptSecretKey();
|
||||
return new AesEncryptAlgorithmApiImpl(encryptSecretKey.getBytes(StandardCharsets.UTF_8));
|
||||
return new AesEncryptAlgorithmApiImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue