Jackson自定義序列化反序列化注解加解密字段詳解
1 問題場景
一些場景中,數(shù)據(jù)庫字段用于存儲(chǔ)json格式數(shù)據(jù),處于安全的考慮,該json數(shù)據(jù)中,某些敏感信息字段需要做加密存儲(chǔ),例如身份證號(hào)、手機(jī)號(hào)等。如果將整個(gè)json數(shù)據(jù)進(jìn)行加密處理,加密后的數(shù)據(jù)較大,只對(duì)需解密的字段進(jìn)行加密處理更符合實(shí)際。
如何實(shí)現(xiàn)呢?
之前寫過一篇脫敏注解,通過自定義序列化實(shí)現(xiàn)的,結(jié)合來看,其實(shí)可以復(fù)用之前的脫敏注解的模式,定義字段加解密注解,通過自定義序列化和反序列化實(shí)現(xiàn)。
2 代碼實(shí)現(xiàn)
注解類
import com.test.jsonencrypt.EncryptJsonFiledDeSerializer;
import com.test.jsonencrypt.EncryptJsonFiledSerializer;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <pre>
* 加解密注解,標(biāo)注在屬性上
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
// 使用自定義的序列化實(shí)現(xiàn)
@JsonSerialize(using = EncryptJsonFiledSerializer.class)
// 使用自定義的序列化實(shí)現(xiàn)
@JsonDeserialize(using = EncryptJsonFiledDeSerializer.class)
public @interface EncryptJsonFiled {
}
自定義序列化實(shí)現(xiàn)類
import com.test.jsonencrypt.annotation.EncryptJsonFiled;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.test.util.EncryUtil;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Objects;
/**
* <pre>
* 序列化注解自定義實(shí)現(xiàn)
* JsonDeserializer<String>:指定String類型,deserialize()方法用于將修改后的數(shù)據(jù)載入
* Jackson使用ContextualSerializer在序列化時(shí)獲取字段注解的屬性
* </pre>
*/
public class EncryptJsonFiledDeSerializer extends JsonDeserializer<String> implements ContextualDeserializer {
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String valueAsString = p.getValueAsString();
// 數(shù)據(jù)不為空時(shí)解密數(shù)據(jù)
if (StringUtils.isNotBlank(valueAsString)) {
// EncryUtil為封裝的加解密工具
return EncryUtil.decrypt(valueAsString);
}
return valueAsString;
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
EncryptJsonFiled annotation = property.getAnnotation(EncryptJsonFiled.class);
// 只針對(duì)String類型
if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
return this;
}
return ctxt.findContextualValueDeserializer(property.getType(), property);
}
}
自定義反序列化實(shí)現(xiàn)類
import com.test.jsonencrypt.annotation.EncryptJsonFiled;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.test.util.EncryUtil;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Objects;
/**
* <pre>
* 序列化注解自定義實(shí)現(xiàn)
* JsonSerializer<String>:指定String類型,serialize()方法用于將修改后的數(shù)據(jù)載入
* Jackson使用ContextualSerializer在序列化時(shí)獲取字段注解的屬性
* </pre>
*/
public class EncryptJsonFiledSerializer extends JsonSerializer<String> implements ContextualSerializer {
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 數(shù)據(jù)不為空時(shí)加密數(shù)據(jù)
if (StringUtils.isNotBlank(value)) {
String encrypt = EncryUtil.encry(value);
gen.writeString(encrypt);
return;
}
gen.writeString(value);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
EncryptJsonFiled annotation = property.getAnnotation(EncryptJsonFiled.class);
// 只針對(duì)String類型
if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
return this;
}
return prov.findValueSerializer(property.getType(), property);
}
}
3 測試
用于測試的簡易實(shí)體類
@Data
@Accessors(chain = true)
public class User {
private String name;
private int age;
@EncryptJsonFiled
private String idCard;
}
測試類
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* <pre>
* 測試加解密注解
* </pre>
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestEntry {
@Test
public void test() throws JsonProcessingException {
User user = new User().setAge(11)
.setName("小明")
.setIdCard("13523451124");
// 注意使用Jackson的json工具
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println("序列化后的json數(shù)據(jù):" + json);
User user1 = objectMapper.readValue(json, User.class);
System.out.println("反序列化后的解密數(shù)據(jù):" + user1.getIdCard());
}
}
測試結(jié)果:
序列化后的json數(shù)據(jù):{"name":"小明","age":11,"idCard":"2f3d8f692eeaac2cbc60423ed99aed63"}
反序列化后的解密數(shù)據(jù):13523451124
到此這篇關(guān)于Jackson自定義序列化反序列化注解加解密字段詳解的文章就介紹到這了,更多相關(guān)Jackson自定義序列化反序列化注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@valid 無法觸發(fā)BindingResult的解決
這篇文章主要介紹了@valid 無法觸發(fā)BindingResult的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot之使用Feign實(shí)現(xiàn)微服務(wù)間的交互
這篇文章主要介紹了SpringBoot中使用Feign實(shí)現(xiàn)微服務(wù)間的交互,對(duì)微服務(wù)這方面感興趣的小伙伴可以參考閱讀本文2023-03-03
Java后端調(diào)用微信支付和支付寶支付的詳細(xì)步驟
這篇文章主要介紹了Java后端如何調(diào)用微信支付和支付寶支付,涵蓋了基本概念、配置步驟、代碼示例以及注意事項(xiàng),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
SpringBoot實(shí)現(xiàn)熱部署的三種方式
本文主要介紹了SpringBoot實(shí)現(xiàn)熱部署的三種方式,主要包括配置pom.xml文件,使用插件的執(zhí)行命令mvn spring-boot:run啟動(dòng)項(xiàng),使用springloader本地啟動(dòng)修改jvm參數(shù),使用devtools工具包,感興趣的可以了解一下2023-12-12
從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機(jī)制
這篇文章主要介紹了從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機(jī)制,帶有GC是Java語言的重要特性之一,需要的朋友可以參考下2015-11-11
java 讀取網(wǎng)頁內(nèi)容的實(shí)例詳解
這篇文章主要介紹了java 讀取網(wǎng)頁內(nèi)容的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09
java通過jacob實(shí)現(xiàn)office在線預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了java通過jacob實(shí)現(xiàn)office在線預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

