Mybatis資料庫欄位加解密2-使用typeAlias實現

weixin_33912246發表於2018-01-01

系列文章

  1. Mybatis資料庫欄位加解密1-使用mysql自帶加密方法
  2. Mybatis資料庫欄位加解密2-使用typeAlias實現

簡介

本文以使用者表為例,介紹如何使用Mybatis的TypeAlias和TypeHandler,對MySql資料庫表的欄位進行加解密。

實現原理

  • 本文使用MyBatis TypeHandler 可以在 JavaType 和 JdbcType 中互相轉換的特性,攔截 JavaType 為 AESEncrypt 的SQL,在預處理語句(PreparedStatement)中設定引數時自動加密,並在結果集(ResultSet)中取值時自動解密。
  • 提供加解密方法,方法相容mysql自帶 的AES_ENCRYPT和AES_DECRYPT方法,所以可以直接使用mysql命令進行驗證。
加密方式: hex(AES_ENCRYPT(#{username}, '${AES_KEY}'))
解密方式: AES_DECRYPT(unhex(username), '${AES_KEY}') 
  • 提供註冊javaType和typeHandler的方法,以便動態增加typeAlias。主要是通過讀取aliases包和handlers包路徑下的類檔案,進行註冊。

實現過程

載入typeAlias和typeHandle類

4183095-3f7402ea0d3a777e.png
photo1.png
  1. 建立typeAliases和typeHandles兩個package
  2. 建立AESEncrypt.java
    該類定義一個名為AESEncrypt的javaType,用於和jdbcType進行相互對映。
import org.apache.ibatis.type.Alias;
@Alias("AESEncrypt")
public class AESEncrypt {
}
  1. 建立AESTypeHandler.java
    該類用於處理javaType為AESEncrypt的資料庫欄位,對插入操作的相關欄位進行加密,對檢索操作的結果進行解密。
@MappedTypes(AESEncrypt.class)
public class EncryptTypeHandler extends BaseTypeHandler<String> {
    private static final Logger LOG = LoggerFactory.getLogger(EncryptTypeHandler.class);
    private static final String aesKey = "emosskgkey";
    /**
     * 用於定義在Mybatis設定引數時該如何把Java型別的引數轉換為對應的資料庫型別
     *
     * @param ps        當前的PreparedStatement物件
     * @param i         當前引數的位置
     * @param parameter 當前引數的Java物件
     * @param jdbcType  當前引數的資料庫型別
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
            throws SQLException {
        // 只要 parameter 非空都進行加密
        LOG.info("setNonNullParameter index <{}>, param <{}> ", i, parameter);
        ps.setString(i, EndecryptUtil.AESEncrypt(parameter,  aesKey));
    }

    /**
     * 用於在Mybatis獲取資料結果集時如何把資料庫型別轉換為對應的Java型別
     *
     * @param rs         當前的結果集
     * @param columnName 當前的欄位名稱
     * @return 轉換後的Java物件
     * @throws SQLException
     */
    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String r = rs.getString(columnName);
        return r == null ? null : EndecryptUtil.AESDecrypt(r, aesKey);
    }

    /**
     * 用於在Mybatis通過欄位位置獲取欄位資料時把資料庫型別轉換為對應的Java型別
     *
     * @param rs          當前的結果集
     * @param columnIndex 當前欄位的位置
     * @return 轉換後的Java物件
     * @throws SQLException
     */
    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String r = rs.getString(columnIndex);
        return r == null ? null : EndecryptUtil.AESDecrypt(r, aesKey);
    }

    /**
     * 用於Mybatis在呼叫儲存過程後把資料庫型別的資料轉換為對應的Java型別
     *
     * @param cs          當前的CallableStatement執行後的CallableStatement
     * @param columnIndex 當前輸出引數的位置
     * @return
     * @throws SQLException
     */
    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String r = cs.getString(columnIndex);
        // 相容待修復的資料
        return r == null ? null : EndecryptUtil.AESDecrypt(r, aesKey);
    }
}
  1. 在SqlSession中註冊typeAliases
  • 配置SqlSession:
    TYPE_ALIAS_PACKAGE_PATH為Alias類存放目錄路徑,如com.xxx.mybatis.typeAliases
    TYPE_HANDLE_PACKAGE_PATH為Handler類存放的目錄路徑,如com.xxx.mybatis.typeHandles
PooledDataSource dataSource = new PooledDataSource();
......
Environment environment = new Environment("development", transactionFactory, dataSource);
// import org.apache.ibatis.session.Configuration
final String key = "AES_KEY"; // sqlSession中全域性屬性kv中的key
String aesKey = "aesKey"; // aes加密使用的key
Configuration configuration = new Configuration(environment);
configuration.setMapUnderscoreToCamelCase(true); //資料庫中下劃線方式的鍵將對映到java pojo中的駝峰命名法的屬性。如user_id對映為userId。
// 註冊typeAliases
registryTypeAlias(configuration, TYPE_ALIAS_PACKAGE_PATH, TYPE_HANDLE_PACKAGE_PATH);
......
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
  • registryTypeAlias方法:
 /**
     * 註冊mybatis的自定義javaType和相應的typeHandler
     * 當aliasPackage和handlePackage為null,直接return。
     * @param configuration
     */
    private void registryTypeAlias(Configuration configuration, String aliasPackage, String handlePackage) {
        if(aliasPackage == null || handlePackage == null)
            return;

        TypeAliasRegistry typeAliasRegistry = configuration.getTypeAliasRegistry();
        typeAliasRegistry.registerAliases(aliasPackage);

        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
//        typeHandlerRegistry.register(AESEncrypt.class, EncryptTypeHandler.class);
        typeHandlerRegistry.register(handlePackage);
    }
  • 欄位加解密方法
    採用AES進行加解密。
/**
     * 使用aes加密
     * 功能和mysql的hex(AES_ENCRYPT(content,'key'))一樣,使用utf-8編碼
     *
     * @param content
     * @param key
     * @return
     */
    public static String AESEncrypt(String content, String key) {
        try {
            final Cipher encryptCipher = Cipher.getInstance("AES");
            SecretKeySpec secretKeySpec = generateMySQLAESKey(key, defaultCharset);
            encryptCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

            byte valBytes[] = encryptCipher.doFinal(content.getBytes(defaultCharset));
            return new String(Hex.encodeHex(valBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用aes解密,
     * 功能和mysql的AES_DECRYPT(unhex(content),'key')一樣,使用utf-8編碼
     *
     * @param content
     * @param key
     * @return
     */
    public static String AESDecrypt(String content, String key) {
        try {
            final Cipher decryptCipher = Cipher.getInstance("AES");
            SecretKeySpec secretKeySpec = generateMySQLAESKey(key, defaultCharset);
            decryptCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte valBytes[] = decryptCipher.doFinal(Hex.decodeHex(content.toCharArray()));
            return new String(valBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 使用aes加密,使用預設key
     * 功能和mysql的hex(AES_ENCRYPT(content,'key'))一樣,使用utf-8編碼
     *
     * @param content
     * @return
     */
    public static String AESEncrypt(String content) {
        return AESEncrypt(content, KEY);
    }

    /**
     * 使用aes解密,使用預設key。
     * 功能和mysql的AES_DECRYPT(unhex(content),'key')一樣,使用utf-8編碼
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String AESDecrypt(String content) {
        return AESDecrypt(content, KEY);
    }

    /**
     * @param key
     * @param encoding
     * @return
     */
    public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
        try {
            final byte[] finalKey = new byte[16];
            int i = 0;
            for (byte b : key.getBytes(encoding))
                finalKey[i++ % 16] ^= b;
            return new SecretKeySpec(finalKey, "AES");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

Mapper.xml使用自定義的javaType

<!-- select: 在 resultMap 或 SQL 中需要加密的欄位上宣告 `javaType="AESEncrypt"` -->
<resultMap id="BaseResultMap" type="user">
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="username" javaType="string" jdbcType="VARCHAR" property="username" />
    <result column="password" javaType="AESEncrypt" jdbcType="VARCHAR" property="password" />
</resultMap>

<!-- insert: 在 SQL 中需要加密的欄位上宣告 `javaType="AESEncrypt"` -->
<insert id="insert" parameterType="user">
    insert into user_t (id, username, password)
    values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password, javaType=AESEncrypt, jdbcType=VARCHAR})
</insert>

<!-- update: 在 SQL 中需要加密的欄位上宣告 `javaType="AESEncrypt"` -->
<update id="update" parameterType="user">
    update user_t set password=#{password, javaType=AESEncrypt, jdbcType=VARCHAR} where id=#{id}
</update>

<!-- 檢索使用者列表, 通過BaseResultMap進行加解密-->
 <select id="getUserList" parameterType="map" resultMap="BaseResultMap">
       select * from user_t
 </select>

參考文獻


本文作者: seawish
版權宣告: 本部落格所有文章除特別宣告外,均採用 CC BY-NC-SA 3.0 許可協議。轉載請註明出處!

相關文章