java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot MyBatis数据库字段加密

SpringBoot结合MyBatis实现数据库字段加密

作者:子墨老师

这篇文章主要为大家详细介绍了SpringBoot如何结合MyBatis实现数据库字段加密功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下

今天分享一篇基于SpringBoot+MyBatis来实现数据库字段加密的操作,喜欢的朋友可以借鉴

大致的实现流程

业务层-->系统拦截器-->数据库-->系统拦截器-->返回结果

加密注解设计

把需要加密的字段通过我们自定义的加密注解进行标识,所以我们需要先自定义一段加密注解的代码

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Encrypt {
}

实体类

在实体类上使用注解标记需要加密字段

@Data
public class User {
    private Long id;
    private String username;
    
    @Encrypt
    private String password;
    
    @Encrypt
    private String email;
    
    @Encrypt
    private String phone;
}

加密工具类

基于AES加密算法实现对字段名的加密,大家可以选择其他的加密算法

public class EncryptionUtil {
    privatestaticfinal String ALGORITHM = "AES";
    privatestaticfinal String TRANSFORMATION = "AES/ECB/PKCS5Padding";
    
    // AES加密
    public static String encrypt(String plainText, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            thrownew RuntimeException("加密失败", e);
        }
    }
    
    // AES解密
    public static String decrypt(String cipherText, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
            returnnew String(decryptedBytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            thrownew RuntimeException("解密失败", e);
        }
    }
}

系统拦截器设计

通过拦截实现自动加密和自动解密

// 加密拦截器
@Intercepts({
    @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
@Component
public class FieldEncryptionInterceptor implements Interceptor {
    
    @Value("${encryption.key:mySecretKey12345}")
    private String encryptionKey;
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];
        Object parameter = args[1];
        
        // 获取SQL命令类型
        String sqlCommandType = mappedStatement.getSqlCommandType().toString();
        
        // 对INSERT和UPDATE操作进行加密处理
        if ("INSERT".equals(sqlCommandType) || "UPDATE".equals(sqlCommandType)) {
            encryptFields(parameter);
        }
        
        return invocation.proceed();
    }
    
    // 解密拦截器
    @Intercepts({
        @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
    })
    @Component
    public class FieldDecryptionInterceptor implements Interceptor {
        
        @Value("${encryption.key:mySecretKey12345}")
        private String encryptionKey;
        
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            // 执行原始方法
            Object result = invocation.proceed();
            
            // 对查询结果进行解密处理
            if (result instanceof List) {
                List<?> list = (List<?>) result;
                for (Object item : list) {
                    decryptFields(item);
                }
            } else {
                decryptFields(result);
            }
            
            return result;
        }
    }
}

测试场景

注意事项

到此这篇关于SpringBoot结合MyBatis实现数据库字段加密的文章就介绍到这了,更多相关SpringBoot MyBatis数据库字段加密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文