├── install.sh
├── .gitignore
├── src
└── main
│ └── java
│ └── org
│ └── apache
│ └── ibatis
│ ├── annotations
│ └── CryptField.java
│ └── plugin
│ └── CryptInterceptor.java
├── LICENSE
├── pom.xml
└── README.md
/install.sh:
--------------------------------------------------------------------------------
1 | mvn clean install source:jar -Dmaven.test.skip=true -Dpackagedir=
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Eclipse project files
2 | .classpath
3 | .project
4 | .settings/
5 |
6 |
7 | # Intellij project files
8 | *.iml
9 | .idea/
10 |
11 | # Others
12 | target/
13 | logs/
14 | tmp/
15 | lib/
--------------------------------------------------------------------------------
/src/main/java/org/apache/ibatis/annotations/CryptField.java:
--------------------------------------------------------------------------------
1 | package org.apache.ibatis.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | /**
6 | * 项目:mybatis-crypt
7 | * 包名:org.apache.ibatis.annotations
8 | * 功能:
9 | * 时间:2017-11-22
10 | * 作者:miaoxw
11 | */
12 | @Documented
13 | @Retention(RetentionPolicy.RUNTIME)
14 | @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
15 | public @interface CryptField {
16 |
17 | String value() default "";
18 |
19 | boolean encrypt() default true;
20 |
21 | boolean decrypt() default true;
22 | }
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 miaoxw
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.lico
8 | mybatis-crypt
9 | 1.0
10 |
11 |
12 | 1.8
13 | UTF-8
14 | UTF-8
15 |
16 |
17 |
18 |
19 | org.mybatis
20 | mybatis
21 | 3.5.6
22 | provided
23 |
24 |
25 | org.apache.commons
26 | commons-lang3
27 | 3.6
28 | provided
29 |
30 |
31 |
32 |
33 |
34 |
35 | maven-compiler-plugin
36 |
37 | 1.8
38 | 1.8
39 | utf-8
40 |
41 | 3.7.0
42 |
43 |
44 | maven-resources-plugin
45 |
46 | utf-8
47 |
48 | 3.0.2
49 |
50 |
51 | maven-source-plugin
52 | 3.0.1
53 |
54 | true
55 |
56 |
57 |
58 | compile
59 |
60 | jar
61 |
62 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # mybatis-crypt
2 | mybatis 拦截器--实现数据库数据脱敏
3 |
4 |
5 |
6 | ### 介绍
7 |
8 | 注解类 @CryptField — 可作用于类成员变量、方法、方法参数
9 |
10 | ```java
11 | @Documented
12 | @Retention(RetentionPolicy.RUNTIME)
13 | @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
14 | public @interface CryptField {
15 |
16 | String value() default "";
17 |
18 | boolean encrypt() default true;
19 |
20 | boolean decrypt() default true;
21 | }
22 | ```
23 |
24 |
25 |
26 |
27 |
28 | ### 使用方法
29 |
30 | 1. 引入jar包
31 |
32 | 2. 配置mybatis插件
33 |
34 | - xml
35 |
36 | ```xml
37 |
38 |
39 |
40 |
41 |
42 |
43 | classpath:mapper/*.xml
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | ```
54 |
55 | - java config
56 |
57 | ```java
58 | @Bean(name = "sqlSessionFactory")
59 | public SqlSessionFactory sqlSessionFactory() throws Exception {
60 | SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
61 | PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
62 | factoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
63 | factoryBean.setDataSource(dataSource);
64 |
65 | List interceptors = Lists.newArrayList();
66 |
67 | CryptInterceptor cryptInterceptor = new CryptInterceptor();
68 | interceptors.add(cryptInterceptor);
69 |
70 | factoryBean.setPlugins(interceptors.toArray(new Interceptor[interceptors.size()]));
71 |
72 | return factoryBean.getObject();
73 | }
74 | ```
75 |
76 |
77 |
78 | 3. 注解使用场景
79 | - 方法
80 |
81 | 表示返回值需要解密,适用对象包括String、List<String>、JavaBean、List<JavaBean>
82 |
83 | ```java
84 | @CryptField
85 | List select(String aaa);
86 | ```
87 |
88 | - 类成员变量
89 |
90 | 表示类成员需要加解密,具体看使用场景、适用对象包括String、List<String>
91 |
92 | - 方法参数
93 |
94 | 表示方法参数需要加密,适用对象包括String、List<String>、JavaBean、List<JavaBean>
95 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/ibatis/plugin/CryptInterceptor.java:
--------------------------------------------------------------------------------
1 | package org.apache.ibatis.plugin;
2 |
3 | import org.apache.commons.lang3.StringUtils;
4 | import org.apache.ibatis.annotations.CryptField;
5 | import org.apache.ibatis.binding.MapperMethod;
6 | import org.apache.ibatis.executor.Executor;
7 | import org.apache.ibatis.mapping.MappedStatement;
8 | import org.apache.ibatis.session.ResultHandler;
9 | import org.apache.ibatis.session.RowBounds;
10 | import org.apache.ibatis.session.defaults.DefaultSqlSession;
11 |
12 | import java.lang.annotation.Annotation;
13 | import java.lang.reflect.Field;
14 | import java.lang.reflect.Method;
15 | import java.util.*;
16 | import java.util.concurrent.ConcurrentHashMap;
17 |
18 | /**
19 | * 项目:mybatis-crypt
20 | * 包名:org.apache.ibatis.plugin
21 | * 功能:数据库数据脱敏
22 | * 加解密算法推荐:aes192 + base64
23 | * 时间:2017-11-22
24 | * 作者:miaoxw
25 | */
26 | @Intercepts({
27 | @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
28 | @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
29 | })
30 | public class CryptInterceptor implements Interceptor {
31 |
32 | private static final String PARAM = "param";
33 |
34 | private static final String PARAM_TYPE_LIST = "list";
35 |
36 | private static final String PARAM_TYPE_COLLECTION = "collection";
37 |
38 | private static final String MAPPEDSTATEMENT_ID_SEPERATOR = ".";
39 |
40 | /**
41 | * 适用于加密判断
42 | */
43 | private static final ConcurrentHashMap> METHOD_PARAM_ANNOTATIONS_MAP = new ConcurrentHashMap<>();
44 | /**
45 | * 适用于解密判断
46 | */
47 | private static final ConcurrentHashMap METHOD_ANNOTATIONS_MAP = new ConcurrentHashMap<>();
48 |
49 | public CryptInterceptor() {
50 |
51 | }
52 |
53 | @Override
54 | public Object intercept(Invocation invocation) throws Throwable {
55 | Object[] args = invocation.getArgs();
56 | // 入参
57 | Object parameter = args[1];
58 | MappedStatement statement = (MappedStatement) args[0];
59 | // 判断是否需要解析
60 | if (!isNotCrypt(parameter)) {
61 | // 单参数 string
62 | if (parameter instanceof String) {
63 | args[1] = stringEncrypt((String) parameter, getParameterAnnotations(statement));
64 | // 单参数 list
65 | } else if (parameter instanceof DefaultSqlSession.StrictMap) {
66 | DefaultSqlSession.StrictMap