SpringBoot整合Hashids實現(xiàn)數(shù)據(jù)ID加密隱藏的全過程
方式一
引入依賴
<dependency>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.3</version>
</dependency>
步驟
1、自定義注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface JsonHashId {
}
2、定義序列化
public class HashIdLongSerializer extends JsonSerializer<Long> implements ContextualSerializer {
@Override
public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(HashIdUtil.encoded(value));
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException {
JsonHashId annotation = property.getAnnotation(JsonHashId.class);
if (annotation != null) {
return this;
} else{
return new NumberSerializers.LongSerializer(Long.class);
}
}
}
3、定義反序列化
public class HashIdLongDeserializer extends JsonDeserializer<Long> implements ContextualDeserializer {
@Override
public Long deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JacksonException {
String value = jsonparser.getText();
return HashIdUtil.decode(value);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext context, BeanProperty property) throws JsonMappingException {
JsonHashId annotation = property.getAnnotation(JsonHashId.class);
if (annotation != null) {
return this;
} else {
return new NumberDeserializers.LongDeserializer(Long.class, null);
}
}
}
4、添加自定義序列化器和反序列化器
public class JacksonObjectMapper extends ObjectMapper {
public JacksonObjectMapper() {
super();
// 收到未知屬性時不報異常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
// 反序列化時,屬性不存在的兼容處理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
module..addDeserializer(Long.class, new HashIdLongDeserializer())
.addSerializer(Long.class, new HashIdLongSerializer());
// 注冊功能模塊 添加自定義序列化器和反序列化器
this.registerModule(module);
}
}
5、配置消息轉(zhuǎn)換器
/**
* 配置
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 擴(kuò)展消息轉(zhuǎn)換器
*
* @param converters
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// 創(chuàng)建消息轉(zhuǎn)換器對象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
// 設(shè)置對象轉(zhuǎn)換器
messageConverter.setObjectMapper(new JacksonObjectMapper());
// 添加到mvc框架消息轉(zhuǎn)換器中,優(yōu)先使用自定義轉(zhuǎn)換器
converters.add(0, messageConverter);
}
}
6、hashids工具類
package com.springboot.api.util;
import org.hashids.Hashids;
/**
* hashid
*/
public class HashIdUtil {
public static Hashids getHashids() {
return new Hashids("hashid", 8);
}
/**
* 加密
*/
public static String encoded(Long id) {
Hashids hashids = getHashids();
return hashids.encode(id);
}
/**
* 解密
*/
public static long decode(String uid) {
Hashids hashids = getHashids();
return hashids.decode(uid)[0];
}
}
測試輸出
userId: 1,
加密后
"userId": "ZxWD0W68",
方式二
定義轉(zhuǎn)換器,將序列化器和反序列化器寫在一起
public class HashIdConverter {
public static class HashIdLongDeserializer extends JsonDeserializer<Long> {
@Override
public Long deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JacksonException {
return HashIdUtil.decode(jsonparser.getText());
}
}
public static class HashIdLongSerializer extends JsonSerializer<Long> {
@Override
public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(HashIdUtil.encoded(value));
}
}
}
配置內(nèi)省器
public class HashIdFieldAnnotationIntrospector extends NopAnnotationIntrospector {
@Override
public Object findSerializer(Annotated annotated) {
TableId annotation = annotated.getAnnotation(TableId.class);
if(annotation != null){
return HashIdConverter.HashIdLongSerializer.class;
} else{
return super.findSerializer(annotated);
}
}
@Override
public Object findDeserializer(Annotated annotated) {
TableId annotation = annotated.getAnnotation(TableId.class);
if(annotation != null){
return HashIdConverter.HashIdLongDeserializer.class;
} else{
return super.findDeserializer(annotated);
}
}
}
配置
public class JacksonObjectMapper extends ObjectMapper {
public JacksonObjectMapper() {
super();
// 將自定義注解內(nèi)省器加入到Jackson注解內(nèi)省器集合里,AnnotationIntrospector是雙向鏈表結(jié)構(gòu)
AnnotationIntrospector annotationIntrospector = this.getSerializationConfig().getAnnotationIntrospector();
AnnotationIntrospector pair = AnnotationIntrospectorPair.pair(annotationIntrospector, new HashIdFieldAnnotationIntrospector());
this.setAnnotationIntrospector(pair);
}
}
最后
到此這篇關(guān)于SpringBoot整合Hashids實現(xiàn)數(shù)據(jù)ID加密隱藏的全過程的文章就介紹到這了,更多相關(guān)SpringBoot Hashids數(shù)據(jù)ID隱藏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis使用useGeneratedKeys獲取自增主鍵的方法
這篇文章主要給大家介紹了關(guān)于Mybatis使用useGeneratedKeys獲取自增主鍵的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java利用redis實現(xiàn)防止接口重復(fù)提交
本文主要為大家詳細(xì)介紹了Java如何利用redis實現(xiàn)防止接口重復(fù)提交,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-11-11
阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
spring?boot集成WebSocket日志實時輸出到web頁面
這篇文章主要為大家介紹了spring?boot集成WebSocket日志實時輸出到web頁面展示的詳細(xì)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

