mybatis拦截器进行数据加密解密

mybatis拦截器进行数据加密解密

package com.chashiyu.mybatisPlugin;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by shiyu.zha on 2018/11/21
 * 用户信息加密
 */
@Slf4j
@Component
@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}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
})
public class MybatisInterceptorByEncrypt implements Interceptor {
    @Value("${facepay.data.encryptKey}")
    private String encryptKey;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
        String methodName = invocation.getMethod().getName();
        Object parameter = invocation.getArgs()[1];
        BoundSql sql = statement.getBoundSql(parameter);
        log.debug("======sql is {}", sql.getSql().replaceAll("(\n)+", "").replaceAll(" +", " "));
        log.debug("======param is {}", JSON.toJSONString(parameter));
        //加密数据
        encryptByOrder(methodName, parameter, sql);
        //解密返回的数据
        Object returnValue = invocation.proceed();
        if (returnValue instanceof ArrayList<?>) {
            List<?> list = (ArrayList<?>) returnValue;
            for (Object val : list) {
                decryptByOrder(val);
            }
        }
        return returnValue;
    }

    private void encryptByOrder(String methodName, Object parameter, BoundSql sql) throws Throwable {
        if (!sql.getSql().contains("o_order")) {
            return;
        }
        if (parameter instanceof PayOrder) {
            PayOrder payOrder = (PayOrder) parameter;
            if (methodName.equals("update")) {
                if (StringUtils.isNotBlank(payOrder.getUserBankNumber())) {
                    payOrder.setUserBankNumber(AESUtils.encrypt(payOrder.getUserBankNumber(), encryptKey));
                }
            }
        }
        if (parameter instanceof PayOrderExample) {
            PayOrderExample payOrderExample = (PayOrderExample) parameter;
            List<PayOrderExample.Criteria> criteriaList = payOrderExample.getOredCriteria();
            for (PayOrderExample.Criteria criteria : criteriaList) {
                List<PayOrderExample.Criterion> criterionList = criteria.getCriteria();
                for (PayOrderExample.Criterion criterion : criterionList) {
                    if (criterion.getCondition().contains("user_bank_number")) {
                        Field field = criterion.getClass().getDeclaredField("value");
                        field.setAccessible(true);
                        field.set(criterion, AESUtils.encrypt(String.valueOf(criterion.getValue()), encryptKey));
                    }
                }
            }
        }
    }
    private void decryptByOrder(Object val) throws Throwable {
        if (val instanceof PayOrder) {
            PayOrder payOrder = (PayOrder) val;
            if(payOrder.getUserBankNumber() != null)payOrder.setUserBankNumber(
                    AESUtils.decrypt(payOrder.getUserBankNumber(), encryptKey));
        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

你可能感兴趣的:(笔记)