编码技巧——数据加密(二)Mybatis拦截器

上一篇《数据加密(一)ShardingSphere》介绍了基于ShardingSphere的数据加密方案,本篇介绍基于Mybatis的加解密插件的原理及实现,以及遇到的困难;

为便于理解,还是先从背景开始介绍;

1. 需求背景

接到公司合规部门的指示,应工信部整改文件,限制各大互联网公司对用户隐私数据的收集;公司出于安全合规的考虑,需要对在数据库中的部分用户(明文存储)信息字段进行加密,防止未经授权的访问以及个人信息泄漏。

由于部分项目架构较老,未使用公司最新的ORM中间件框架(基于sharding-jdbc),因此与公司内部提供的加解密方案(shardingsphere)不兼容,改造的成本太大,因此我们选用了Mybatis插件来实现数据库加解密,保证往数据库写入数据时能对指定字段加密,读取数据时能对指定字段解密。

2. 思路与方案

2.1 系统设计架构

  1. 对每个需要加密的字段新增密文字段(对业务有侵入),修改数据库、mapper.xml以及DO对象,通过插件的方式把针对明文/密文字段的加解密进行收口。
  2. 自定义Executor对SELECT/UPDATE/INSERT/DELETE等操作的明文字段进行加密并设置到密文字段。
  3. 自定义插件ResultSetHandler负责针对查询结果进行解密,负责对SELECT等操作的密文字段进行解密并设置到明文字段。

2.2 系统执行流程

  1. 新增加解密流程控制开关,分别控制写入时是只写原字段/双写/只写加密后的字段,以及读取时是读原字段还是加密后的字段。
  2. 新增历史数据加密任务,对历史数据批量进行加密,写入到加密后字段。
  3. 出于安全上的考虑,流程里还会有一些校验/补偿的任务,这里不再赘述。

3. 方案制定

先简单介绍mybatis插件,然后分析mybatis的总体执行流程从而明确插件在何时织入、能做哪些事情,接着介绍并选择合适的插件类型,最终从而确定我们的方案——使用哪些拦截器及拦截器里需要做哪些事情;

3.1 Mybatis插件简介

Mybatis 预留了 org.apache.ibatis.plugin.Interceptor 接口,通过实现该接口,我们能对Mybatis的执行流程进行拦截,接口的定义如下:

public interface Interceptor {
 
  Object intercept(Invocation invocation) throws Throwable;
 
  Object plugin(Object target);
 
  void setProperties(Properties properties);
 
}

其中有三个方法:

  • intercept: 插件执行的具体流程,传入的Invocation是Mybatis对被代理的方法的封装;
  • plugin: 使用当前的Interceptor创建代理,通常的实现都是 Plugin.wrap(target, this),wrap方法内使用 jdk 创建动态代理对象;
  • setProperties: 参考下方代码,在Mybatis配置文件中配置插件时可以设置参数,在setProperties函数中调用 Properties.getProperty("param1") 方法可以得到配置的值
<plugins>
    <plugin interceptor="com.xx.xx.xxxInterceptor">
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

在实现intercept函数对Mybatis的执行流程进行拦截前,我们需要使用@Intercepts注解指定拦截的方法。

@Intercepts({ 
        @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) 
})

参考上方代码,我们可以指定需要拦截的类和方法。当然我们不能对任意的对象做拦截,Mybatis插件可拦截的类为以下四个:

  • Executor
  • StatementHandler
  • ParameterHandler
  • ResultSetHandler

回到数据库加密的需求,我们需要从上面4个类里选择能用来实现入参加密和出参解密的类。在介绍这四个类之前,需要对Mybatis的执行流程有一定的了解。

3.2 Mybatis整体执行流程 

1. spring通过sqlSessionFactoryBean创建sqlSessionFactory,在使用sqlSessionFactoryBean时,我们通常会指定configLocation和mapperLocations,来告诉sqlSessionFactoryBean去哪里读取配置文件以及去哪里读取mapper文件。

2. 得到配置文件和mapper文件的位置后,分别调用XmlConfigBuilder.parse()和XmlMapperBuilder.parse()创建Configuration和MappedStatement,Configuration类顾名思义,存放的是Mybatis所有的配置,而MappedStatement类存放的是每条sql语句的封装,MappedStatement以map的形式存放到Configuration对象中,key为对应方法的全路径。

3. spring通过ClassPathMapperScanner扫描所有的Mapper接口,为其创建BeanDefinition对象,但由于他们本质上都是没有被实现的接口,所以spring会将他们的BeanDefinition的beanClass属性修改为MapperFactorybean。

4. MapperFactoryBean也实现了FactoryBean接口,spring在创建Bean时会调用FactoryBean.getObject()方法获取Bean,最终是通过mapperProxyFactory的newInstance方法为mapper接口创建代理,创建代理的方式是JDK,最终生成的代理对象是MapperProxy。

5. 调用mapper的所有接口本质上调用的都是MapperProxy.invoke方法,内部调用sqlSession的insert/update/delete等各种方法。

// MapperMethod.java
public Object execute(SqlSession sqlSession, Object[] args) {
  Object result;
  if (SqlCommandType.INSERT == command.getType()) {
    Object param = method.convertArgsToSqlCommandParam(args);
    result = rowCountResult(sqlSession.insert(command.getName(), param));
  } else if (SqlCommandType.UPDATE == command.getType()) {
    Object param = method.convertArgsToSqlCommandParam(args);
    result = rowCountResult(sqlSession.update(command.getName(), param));
  } else if (SqlCommandType.DELETE == command.getType()) {
    Object param = method.convertArgsToSqlCommandParam(args);
    result = rowCountResult(sqlSession.delete(command.getName(), param));
  } else if (SqlCommandType.SELECT == command.getType()) {
    if (method.returnsVoid() && method.hasResultHandler()) {
      executeWithResultHandler(sqlSession, args);
      result = null;
    } else if (method.returnsMany()) {
      result = executeForMany(sqlSession, args);
    } else if (method.returnsMap()) {
      result = executeForMap(sqlSession, args);
    } else {
      Object param = method.convertArgsToSqlCommandParam(args);
      result = sqlSession.selectOne(command.getName(), param);
    }
  } else if (SqlCommandType.FLUSH == command.getType()) {
      result = sqlSession.flushStatements();
  } else {
    throw new BindingException("Unknown execution method for: " + command.getName());
  }
  if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
    throw new BindingException("Mapper method '" + command.getName()
        + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
  }
  return result;
}


6. SqlSession可以理解为一次会话,SqlSession会从Configuration中获取对应的MappedStatement,交给Executor执行。

// DefaultSqlSession.java
@Override
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  try {
    // 从configuration对象中使用被调用方法的全路径,获取对应的MappedStatement
    MappedStatement ms = configuration.getMappedStatement(statement);
    return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
  } catch (Exception e) {
    throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}


7. Executor会先创建StatementHandler,StatementHandler可以理解为是一次语句的执行。

8. 然后Executor会获取连接,具体获取连接的方式取决于Datasource的实现,可以使用连接池等方式获取连接。

9. 之后调用StatementHandler.prepare方法,对应到jdbc执行流程中的Connection.prepareStatement这一步。

10. Executor再调用StatementHandler的parameterize方法,设置参数,对应到jdbc执行流程的StatementHandler.setXXX()设置参数,内部会创建ParameterHandler方法。

// SimpleExecutor.java
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    // 创建StatementHandler,对应第7步
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    // 获取连接,再调用conncetion.prepareStatement创建prepareStatement,设置参数
    stmt =
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值