Java如何对数据库敏感字段快速方便地加密解密?本文使用的加密算法是SM4国密算法
工具/原料
Java编译器
加密解密工具类
1、首先在pom.xml文件中导入依赖:
2、加解密的工具类:(篇幅限制,分成两张图)
加密解密处理器
1、先写加密解密处理器,如下图
2、第一步:在Mapper.xml的resultMap标签中使用,使用示例:需要加密的字段加上typehandler属性---<resu造婷用痃ltMap id="BaseResultMap1" type="com.thunisoft.dzsjdc.repository.model.TYwSqjbxx"> <id column="c_id" jdbcType="CHAR" property="cId"/> <result column="c_bdcrxm" jdbcType="VARCHAR" property="cBdcrxm" typeHandler="com......SMCryptoTypeHandler"/> <result column="c_bdcrsf" jdbcType="VARCHAR" property="cBdcrsf" typeHandler="com......SMCryptoTypeHandler"/> <result column="c_bdcrzw" jdbcType="VARCHAR" property="cBdcrzw" typeHandler="com......SMCryptoTypeHandler"/> <result column="c_saay" jdbcType="VARCHAR" property="cSaay"/> <result column="c_bwtdwid" jdbcType="VARCHAR" property="cBwtdwid"/></resultMap>---
3、第二步:实体类的对应敏感字段上加上注解@ColumnType,示例如下:---@Column(name = "c_bdcrxm")@ApiModelProperty("...")@ColumnType(typeHandler = SMCryptoTypeHandler.class)private String cBdcrxm;---
4、第三步:敏感字段作为筛选条件的查询语句、敏感字段插入数据库时,做出如下处理:---<坡纠课柩if test="稆糨孝汶;map.bdcrxm != null and map.bdcrxm != ''"> and c_bdcrxm like concat('%', #{map.bdcrxm,jdbcType=VARCHAR, typeHandler=com...路径...SMCryptoTypeHandler},'%')</if>---