<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          扔掉工具類!MyBatis 一個簡單配置搞定加密、解密,不能太方便了~!

          共 7344字,需瀏覽 15分鐘

           ·

          2022-09-15 18:05

          往期熱門文章:

          1、小公司里用SpringBoot做MySQL分庫分表,踩了一些坑!
          2、沒有幾十年功力,寫不出這一行“看似無用”的代碼?。?/a>
          3、為什么 Spring和IDEA 都不推薦使用 @Autowired 注解
          4、從阿里跳槽來的工程師,寫個Controller都這么優(yōu)雅!
          5、try catch真的會影響性能?居然被騙了好幾年...

          源:juejin.cn/post/6963811586184052767



          前言:介紹一個簡單的MyBatis加解密方式,日常學習工作中提及這種方法的比較少,所以拿來說說,如果已經(jīng)知道這種方法的忽略本文!

          一、背景

          在我們數(shù)據(jù)庫中有些時候會保存一些用戶的敏感信息,比如:手機號、銀行卡等信息,如果這些信息以明文的方式保存,那么是不安全的。假如:黑客黑進了數(shù)據(jù)庫,或者離職人員導出了數(shù)據(jù),那么就可能導致這些敏感數(shù)據(jù)的泄漏。因此我們就需要找到一種方法來解決這個問題。

          二、解決方案

          由于我們系統(tǒng)中使用了Mybatis作為數(shù)據(jù)庫持久層,因此決定使用Mybatis的TypeHandler或Plugin來解決。

          TypeHandler :  需要我們在某些列上手動指定 typeHandler 來選擇使用那個typeHandler或者根據(jù)@MappedJdbcTypes 和 @MappedTypes注解來自行推斷。

          <result column="phone" property="phone" 
          typeHandler="com.huan.study.mybatis.typehandler.EncryptTypeHandler"/>

          Plugin : 可以攔截系統(tǒng)中的 select、insert、update、delete等語句,也能獲取到sql執(zhí)行前的參數(shù)和執(zhí)行后的數(shù)據(jù)。

          經(jīng)過考慮,決定使用TypeHandler來加解密數(shù)據(jù)。

          三、需求

          我們有一張客戶表customer,里面有客戶手機號(phone)和客戶地址(address)等字段,其中客戶手機號(phone)是需要加密保存到數(shù)據(jù)庫中的。

          1、在添加客戶信息時,自動將客戶手機號加密保存到數(shù)據(jù)中。

          2、在查詢客戶信息時,自動解密客戶手機號。

          四、實現(xiàn)思路

          1、編寫一個實體類,凡是此實體類的數(shù)據(jù)都表示需要加解密的

          public class Encrypt {
              private String value;

              public Encrypt() {
              }

              public Encrypt(String value) {
                  this.value = value;
              }

              public String getValue() {
                  return value;
              }

              public void setValue(String value) {
                  this.value = value;
              }
          }

          2、編寫一個加解密的TypeHandler

          • 設(shè)置參數(shù)時,加密數(shù)據(jù)。
          • 從數(shù)據(jù)庫獲取記錄時,解密數(shù)據(jù)。
          package com.huan.study.mybatis.typehandler;

          import cn.hutool.crypto.SecureUtil;
          import cn.hutool.crypto.symmetric.AES;
          import org.apache.ibatis.type.BaseTypeHandler;
          import org.apache.ibatis.type.JdbcType;
          import org.apache.ibatis.type.MappedJdbcTypes;
          import org.apache.ibatis.type.MappedTypes;

          import java.nio.charset.StandardCharsets;
          import java.sql.CallableStatement;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;

          /**
           * 加解密TypeHandler
           *
           * @author huan.fu 2021/5/18 - 上午9:20
           */

          @MappedJdbcTypes(JdbcType.VARCHAR)
          @MappedTypes(Encrypt.class)
          public class EncryptTypeHandler extends BaseTypeHandler<Encrypt
          {

              private static final byte[] KEYS = "12345678abcdefgh".getBytes(StandardCharsets.UTF_8);

              /**
               * 設(shè)置參數(shù)
               */

              @Override
              public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {
                  if (parameter == null || parameter.getValue() == null) {
                      ps.setString(i, null);
                      return;
                  }
                  AES aes = SecureUtil.aes(KEYS);
                  String encrypt = aes.encryptHex(parameter.getValue());
                  ps.setString(i, encrypt);
              }

              /**
               * 獲取值
               */

              @Override
              public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
                  return decrypt(rs.getString(columnName));
              }

              /**
               * 獲取值
               */

              @Override
              public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
                  return decrypt(rs.getString(columnIndex));
              }

              /**
               * 獲取值
               */

              @Override
              public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
                  return decrypt(cs.getString(columnIndex));
              }

              public Encrypt decrypt(String value) {
                  if (null == value) {
                      return null;
                  }
                  return new Encrypt(SecureUtil.aes(KEYS).decryptStr(value));
              }
          }

          注意??:

          • @MappedTypes:表示該處理器處理的java類型是什么。
          • @MappedJdbcTypes:表示處理器處理的Jdbc類型。

          3、sql語句中寫法

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
          <mapper namespace="com.huan.study.mybatis.mappers.CustomerMapper">

              <resultMap id="BaseResultMapper" type="com.huan.study.mybatis.entity.Customer">
                  <id column="id" property="id"/>
                  <result column="phone" property="phone"/>
                  <result column="address" property="address"/>
              </resultMap>

              <insert id="addCustomer">
                  insert into customer(phone,address) values (#{phone},#{address})
              </insert>

              <select id="findCustomer" resultMap="BaseResultMapper">
                  select * from customer where phone = #{phone}
              </select>

          </mapper>

          SQL中沒有什么特殊的寫法。

          4、配置文件中指定Typehandler的包路徑

          mybatis.type-handlers-package=com.huan.study.mybatis.typehandler

          5、編寫后臺代碼

          • 提供一個添加方法
          • 提供一個根據(jù)手機號查詢的方法

          后臺代碼比較簡單,直接查看:

          https://gitee.com/huan1993/spring-cloud-parent/tree/master/mybatis/mybatis-typehandler-encrypt

          貼一個mapper層的截圖。

          6、測試結(jié)果

          從測試結(jié)果中可知,添加數(shù)據(jù)時,需要加密的數(shù)據(jù)(phone)在數(shù)據(jù)庫中已經(jīng)加密了,在查詢的時候,加密的數(shù)據(jù)已經(jīng)自動解密了。

          五、實現(xiàn)代碼

          https://gitee.com/huan1993/spring-cloud-parent/tree/master/mybatis/mybatis-typehandler-encrypt


          往期熱門文章:

          1、國產(chǎn)開發(fā)工具的天花板,用來擼項目真香!
          2、這個 MySQL bug 99% 的人會踩坑!
          3、公司產(chǎn)品太多了,怎么實現(xiàn)一次登錄產(chǎn)品互通?
          4、Redis 官方可視化工具,高顏值,功能太強大!
          5、用了BigDecimal就不會資損?了解下BigDecimal這五個坑
          6、一個依賴搞定 Spring Boot 反爬蟲,防止接口盜刷!
          7、千萬不要把 Request 傳遞到異步線程里面!有坑!
          8、不卷了!入職字節(jié)一周就果斷跑了。
          9、SpringBoot+ShardingSphereJDBC實現(xiàn)讀寫分離!
          10、不好意思, Maven 該換了!

          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲中文免费 | 7799天天综合网精品 | 欧美强乱中文字幕在线 | 婷婷男人天堂 | 乱伦无码视频 |