# mybatis-crypto **Repository Path**: vovans/mybatis-crypto ## Basic Information - **Project Name**: mybatis-crypto - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-02-15 - **Last Updated**: 2024-11-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mybatis-crypto GitHub release GitHub last commit GitHub Workflow Status ## 简介 `mybatis-crypto` 是一个基于 mybatis 插件机制实现的字段加解密组件,通过一个注解即可对敏感数据进行加解密处理。 支持自定义 `Encryptor`、特殊字段单独指定 `Encryptor` 和 `key` ,满足大部分使用场景。 ## 模块 `mybatis-crypto` 包括三个模块: - `mybatis-crypto-core` 插件的核心功能模块 - `mybatis-crypto-spring-boot-starter` 提供了 `Spring boot` 快速整合功能 - `mybatis-crypto-encryptors` 提供了一些 IEncryptor 实现 ## 使用方法 1. 引入依赖 ```xml io.github.whitedg mybatis-crypto-spring-boot-starter ${latest.version} ``` 2. 实现 IEncryptor ```java import io.github.whitedg.mybatis.crypto.IEncryptor; public class MyEncryptor implements IEncryptor { @Override public String encrypt(Object val2bEncrypted, String key) throws Exception { // 实现这个方法返回加密后的数据 return "encrypted string"; } @Override public String decrypt(Object val2bDecrypted, String key) throws Exception { // 实现这个方法返回解密后的数据 return "decrypted string"; } } ``` 或者引入 `mybatis-crypto-encryptors` ```xml io.github.whitedg mybatis-crypto-encryptors ${latest.version} ``` 使用其提供的 `Encryptor`: - `io.github.whitedg.mybatis.crypto.Base64Encryptor` - `io.github.whitedg.mybatis.crypto.BasicTextEncryptor` - `io.github.whitedg.mybatis.crypto.AES256Encryptor` - `io.github.whitedg.mybatis.crypto.StrongTextEncryptor` 3. 添加配置 ```yaml mybatis-crypto: # 是否启用插件,默认 true enabled: true # 快速失败,默认 true fail-fast: false # 全局默认 Encryptor default-encryptor: io.github.whitedg.mybatis.crypto.BasicTextEncryptor # Encryptor 默认密钥 default-key: global-key # mybatis @Param 注解下需要加解密的参数 key 前缀 mapped-key-prefixes: et,encrypted ``` 4. 指定加密字段 - 在需要加解密的字段上添加注解 `@EncryptedField` ```java public class User { @EncryptedField private String encryptedStr; @EncryptedField(encryptor = YourEncryptor.class, key = "Your Key") private String customizedStr; } ``` - 使用配置的 @Param 参数 key 前缀 ```java import org.apache.ibatis.annotations.Param; interface YourEntityMapper { int insert(@Param("et") YourEntity entity); // 支持数组 int batchInsert(@Param("encrypted-entities") List entity); // 返回值也支持单个对象或数组 YourEntity selectOne(); List selectList(); } ``` ## [Demo](https://github.com/WhiteDG/mybatis-crypto/blob/main/mybatis-crypto-demo/README.MD) ## 配置项说明 | 配置项 | 说明 | 默认值 | | ---- | ---- | ---- | | mybatis-crypto.enabled | 是否启用 mybatis-crypto | true | | mybatis-crypto.fail-fast | 快速失败,加解密过程中发生异常是否中断。true:抛出异常,false:使用原始值,打印 warn 级别日志 | true | | mybatis-crypto.mapped-key-prefixes | @Param 参数名的前缀,前缀匹配则会进行加密处理 | 空 | | mybatis-crypto.default-encryptor | 全局默认 Encryptor | 空 | | mybatis-crypto.default-key | 全局默认 Encryptor 的密钥 | 空 | ## LICENSE ``` Copyright 2021 WhiteDG 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 https://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. ```