SpringBoot整合Hashids實(shí)現(xiàn)數(shù)據(jù)ID加密隱藏的全過(guò)程
方式一
引入依賴
<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();
// 收到未知屬性時(shí)不報(bào)異常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
// 反序列化時(shí),屬性不存在的兼容處理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
module..addDeserializer(Long.class, new HashIdLongDeserializer())
.addSerializer(Long.class, new HashIdLongSerializer());
// 注冊(cè)功能模塊 添加自定義序列化器和反序列化器
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)換器對(duì)象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
// 設(shè)置對(duì)象轉(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];
}
}
測(cè)試輸出
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實(shí)現(xiàn)數(shù)據(jù)ID加密隱藏的全過(guò)程的文章就介紹到這了,更多相關(guān)SpringBoot Hashids數(shù)據(jù)ID隱藏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?8中讀取文件內(nèi)容?Files.lines()方法使用示例
這篇文章主要介紹了Java?8中讀取文件內(nèi)容Files.lines()方法如何使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
這篇文章主要介紹了Java struts2中如何實(shí)現(xiàn)圖片上傳的相關(guān)資料,需要的朋友可以參考下2015-11-11
使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程
這篇文章主要為大家介紹了使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Springboot工具類FileCopyUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之FileCopyUtils的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
Java并發(fā)編程之synchronized底層實(shí)現(xiàn)原理分析
這篇文章主要介紹了Java并發(fā)編程之synchronized底層實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
IDEA編譯時(shí)報(bào)常量字符串過(guò)長(zhǎng)的解決辦法
本文主要介紹了IDEA編譯時(shí)報(bào)常量字符串過(guò)長(zhǎng)的解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

