亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis給數(shù)據(jù)庫(kù)敏感字段加解密詳解

 更新時(shí)間:2023年11月23日 09:59:06   作者:chaojunma  
這篇文章主要介紹了Mybatis給數(shù)據(jù)庫(kù)敏感字段加解密詳解,為了保護(hù)數(shù)據(jù)庫(kù)敏感字段數(shù)據(jù)安全,有時(shí)候我們需要將敏感數(shù)據(jù)加密入庫(kù),查詢(xún)時(shí)再解密成明文,我們可以利用Mybatis自定義TypeHandler來(lái)處理,需要的朋友可以參考下

前言

為了保護(hù)數(shù)據(jù)庫(kù)敏感字段數(shù)據(jù)安全,有時(shí)候我們需要將敏感數(shù)據(jù)加密入庫(kù),查詢(xún)時(shí)再解密成明文。

我們可以利用Mybatis自定義TypeHandler來(lái)處理,下面我們來(lái)具體實(shí)現(xiàn)一下。

定義KeyCenterUtils加解密工具類(lèi)

import org.springframework.stereotype.Service;
import java.util.Base64;
 
@Service
public class KeyCenterUtils {
 
    public  String encrypt(String src) {
        try {
            String result = Base64.getEncoder().encodeToString(src.getBytes("UTF-8"));
            return result;
        } catch (Exception e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }
 
    public  String decrypt(String src) {
        try {
            byte[] asBytes = Base64.getDecoder().decode(src);
            String result = new String(asBytes, "UTF-8");
            return result;
        } catch (Exception e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }
}

自定義Handler類(lèi)實(shí)現(xiàn)數(shù)據(jù)庫(kù)字段加解密

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mk.util.KeyCenterUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class CustomTypeHandler<T> extends BaseTypeHandler<T> {
 
    @Autowired
    private KeyCenterUtils keyCenterUtils;
 
    public CustomTypeHandler() {
    }
 
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, this.keyCenterUtils.encrypt((String)parameter));
    }
    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String columnValue = rs.getString(columnName);
        //有一些可能是空字符
        return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
    }
 
    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String columnValue = rs.getString(columnIndex);
        return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
    }
 
    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String columnValue = cs.getString(columnIndex);
        return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
    }
}

因?yàn)槲矣玫氖荕ybatis-Plus,所以可以使用Mybatis-Plus的@TableField的注解通過(guò)typeHandler屬性指定上面自定義的Handler即可。

實(shí)體類(lèi)添加注解

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "lemon_user", autoResultMap = true)
public class User {
 
    @TableId(type = IdType.AUTO)
    private Long id;
 
    private String username;
 
    @TableField(typeHandler = CustomTypeHandler.class)
    private String password;
 
    private String salt;
}

注意:上面的@TableName注解設(shè)置了autoResultMap = true的屬性值,這樣通過(guò)Mybatis-Plus的BaseMapper查詢(xún)出來(lái)的數(shù)據(jù)才會(huì)將加密字段進(jìn)行解密,默認(rèn)不生效。

如果不是Mybatis-Plus的 BaseMapper內(nèi)部的方法,則需要我們?cè)诓樵?xún)時(shí)在resultMap的屬性中指定我們自定義的typeHandler,如下:

<resultMap id="baseResultMap" type="com.mk.entity.User">
    <id property="id" column="id"/>
    <result property="username" column="username"/>
    <result property="password" column="password" typeHandler="com.mk.handler.CustomTypeHandler"/>
</resultMap>
 
<select id="getUserByName" resultMap="baseResultMap">
    select * from lemon_user where username = #{username}
</select>

到此這篇關(guān)于Mybatis給數(shù)據(jù)庫(kù)敏感字段加解密詳解的文章就介紹到這了,更多相關(guān)Mybatis敏感字段加解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解springboot之jackson的兩種配置方式

    詳解springboot之jackson的兩種配置方式

    這篇文章主要介紹了詳解springboot之jackson的兩種配置方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • JavaMail入門(mén)教程之創(chuàng)建郵件(2)

    JavaMail入門(mén)教程之創(chuàng)建郵件(2)

    這篇文章主要介紹了JavaMail入門(mén)教程之創(chuàng)建郵件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • HttpClient基礎(chǔ)解析

    HttpClient基礎(chǔ)解析

    這篇文章主要介紹了HttpClient基礎(chǔ)知識(shí),算是比較詳細(xì)地對(duì)知識(shí)點(diǎn)和相關(guān)實(shí)例進(jìn)行解釋,需要的朋友可以參考下
    2017-09-09
  • 使用注解開(kāi)發(fā)SpringMVC詳細(xì)配置教程

    使用注解開(kāi)發(fā)SpringMVC詳細(xì)配置教程

    這篇文章主要介紹了使用注解開(kāi)發(fā)SpringMVC詳細(xì)配置教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 淺談SpringBoot如何自定義Starters

    淺談SpringBoot如何自定義Starters

    今天帶大家來(lái)學(xué)習(xí)SpringBoot如何自定義Starters,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Java連接數(shù)據(jù)庫(kù)JDBC技術(shù)之prepareStatement的詳細(xì)介紹

    Java連接數(shù)據(jù)庫(kù)JDBC技術(shù)之prepareStatement的詳細(xì)介紹

    這篇文章主要介紹了Java連接數(shù)據(jù)庫(kù)JDBC技術(shù)之prepareStatement的詳細(xì)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏

    淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏

    脫敏是指在不改變?cè)瓟?shù)據(jù)結(jié)構(gòu)的前提下,通過(guò)某種方式處理數(shù)據(jù),使數(shù)據(jù)不能直接暴露用戶(hù)的真實(shí)信息,下面我們就來(lái)看看SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏的具體方法吧
    2024-03-03
  • 帶你重新認(rèn)識(shí)MyBatis的foreach

    帶你重新認(rèn)識(shí)MyBatis的foreach

    這篇文章主要介紹了重新認(rèn)識(shí)MyBatis的foreach,本文提出了一種簡(jiǎn)化<foreach>寫(xiě)法的設(shè)想,更重要的是通過(guò)解決空集時(shí)生成的SQL語(yǔ)法問(wèn)題,更深刻地理解MyBatis的foreach的生成機(jī)制,需要的朋友可以參考下
    2022-11-11
  • 使用JPA自定義id策略避免主鍵自增

    使用JPA自定義id策略避免主鍵自增

    這篇文章主要介紹了使用JPA自定義id策略避免主鍵自增問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java通過(guò)反射機(jī)制動(dòng)態(tài)設(shè)置對(duì)象屬性值的方法

    Java通過(guò)反射機(jī)制動(dòng)態(tài)設(shè)置對(duì)象屬性值的方法

    下面小編就為大家?guī)?lái)一篇Java通過(guò)反射機(jī)制動(dòng)態(tài)設(shè)置對(duì)象屬性值的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07

最新評(píng)論