Jackson自定義序列化反序列化注解加解密字段詳解
1 問(wèn)題場(chǎng)景
一些場(chǎng)景中,數(shù)據(jù)庫(kù)字段用于存儲(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)呢?
之前寫過(guò)一篇脫敏注解,通過(guò)自定義序列化實(shí)現(xiàn)的,結(jié)合來(lái)看,其實(shí)可以復(fù)用之前的脫敏注解的模式,定義字段加解密注解,通過(guò)自定義序列化和反序列化實(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 測(cè)試
用于測(cè)試的簡(jiǎn)易實(shí)體類
@Data
@Accessors(chain = true)
public class User {
private String name;
private int age;
@EncryptJsonFiled
private String idCard;
}
測(cè)試類
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>
* 測(cè)試加解密注解
* </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());
}
}
測(cè)試結(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)文章
Java中常見(jiàn)的查找算法與排序算法總結(jié)
數(shù)據(jù)結(jié)構(gòu)是數(shù)據(jù)存儲(chǔ)的方式,算法是數(shù)據(jù)計(jì)算的方式。所以在開(kāi)發(fā)中,算法和數(shù)據(jù)結(jié)構(gòu)息息相關(guān)。本文為大家整理了Java中常見(jiàn)的查找與排序算法的實(shí)現(xiàn),需要的可以參考一下2023-03-03
java組裝樹(shù)形結(jié)構(gòu)List問(wèn)題
這篇文章主要介紹了java組裝樹(shù)形結(jié)構(gòu)List問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn)
這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
OpenFeign設(shè)置header的三種方式總結(jié)
在微服務(wù)間使用Feign進(jìn)行遠(yuǎn)程調(diào)用時(shí)需要在header中添加信息,下面這篇文章主要給大家介紹了關(guān)于OpenFeign設(shè)置header的三種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問(wèn)題的解決辦法
今天小編就為大家分享一篇關(guān)于實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問(wèn)題的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
java求100之內(nèi)的素?cái)?shù)(質(zhì)數(shù))簡(jiǎn)單示例
這篇文章主要介紹了java求100之內(nèi)的素?cái)?shù)簡(jiǎn)單示例,素?cái)?shù)是一個(gè)大于1的自然數(shù),如果除了1和它自身外,不能被其他自然數(shù)整除的數(shù);否則稱為合數(shù)2014-04-04

