Jackson自定義序列化反序列化注解加解密字段詳解
1 問題場景
一些場景中,數(shù)據(jù)庫字段用于存儲json格式數(shù)據(jù),處于安全的考慮,該json數(shù)據(jù)中,某些敏感信息字段需要做加密存儲,例如身份證號、手機號等。如果將整個json數(shù)據(jù)進行加密處理,加密后的數(shù)據(jù)較大,只對需解密的字段進行加密處理更符合實際。
如何實現(xiàn)呢?
之前寫過一篇脫敏注解,通過自定義序列化實現(xiàn)的,結合來看,其實可以復用之前的脫敏注解的模式,定義字段加解密注解,通過自定義序列化和反序列化實現(xiàn)。
2 代碼實現(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>
* 加解密注解,標注在屬性上
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
// 使用自定義的序列化實現(xiàn)
@JsonSerialize(using = EncryptJsonFiledSerializer.class)
// 使用自定義的序列化實現(xiàn)
@JsonDeserialize(using = EncryptJsonFiledDeSerializer.class)
public @interface EncryptJsonFiled {
}
自定義序列化實現(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>
* 序列化注解自定義實現(xiàn)
* JsonDeserializer<String>:指定String類型,deserialize()方法用于將修改后的數(shù)據(jù)載入
* Jackson使用ContextualSerializer在序列化時獲取字段注解的屬性
* </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ù)據(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);
// 只針對String類型
if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
return this;
}
return ctxt.findContextualValueDeserializer(property.getType(), property);
}
}
自定義反序列化實現(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>
* 序列化注解自定義實現(xiàn)
* JsonSerializer<String>:指定String類型,serialize()方法用于將修改后的數(shù)據(jù)載入
* Jackson使用ContextualSerializer在序列化時獲取字段注解的屬性
* </pre>
*/
public class EncryptJsonFiledSerializer extends JsonSerializer<String> implements ContextualSerializer {
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 數(shù)據(jù)不為空時加密數(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);
// 只針對String類型
if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
return this;
}
return prov.findValueSerializer(property.getType(), property);
}
}
3 測試
用于測試的簡易實體類
@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());
}
}
測試結果:
序列化后的json數(shù)據(jù):{"name":"小明","age":11,"idCard":"2f3d8f692eeaac2cbc60423ed99aed63"}
反序列化后的解密數(shù)據(jù):13523451124
到此這篇關于Jackson自定義序列化反序列化注解加解密字段詳解的文章就介紹到這了,更多相關Jackson自定義序列化反序列化注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
@valid 無法觸發(fā)BindingResult的解決
這篇文章主要介紹了@valid 無法觸發(fā)BindingResult的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot之使用Feign實現(xiàn)微服務間的交互
這篇文章主要介紹了SpringBoot中使用Feign實現(xiàn)微服務間的交互,對微服務這方面感興趣的小伙伴可以參考閱讀本文2023-03-03
java通過jacob實現(xiàn)office在線預覽功能
這篇文章主要為大家詳細介紹了java通過jacob實現(xiàn)office在線預覽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08

