欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Hashids實(shí)現(xiàn)數(shù)據(jù)ID加密隱藏的全過程

 更新時(shí)間:2024年01月28日 10:55:27   作者:彭世瑜  
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合Hashids實(shí)現(xiàn)數(shù)據(jù)ID加密隱藏的全過程,文中的示例代碼講解詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

方式一

引入依賴

<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ān)SpringBoot Hashids數(shù)據(jù)ID隱藏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis使用useGeneratedKeys獲取自增主鍵的方法

    Mybatis使用useGeneratedKeys獲取自增主鍵的方法

    這篇文章主要給大家介紹了關(guān)于Mybatis使用useGeneratedKeys獲取自增主鍵的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java利用redis實(shí)現(xiàn)防止接口重復(fù)提交

    Java利用redis實(shí)現(xiàn)防止接口重復(fù)提交

    本文主要為大家詳細(xì)介紹了Java如何利用redis實(shí)現(xiàn)防止接口重復(fù)提交,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-11-11
  • 阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏

    阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏

    這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • spring?boot集成WebSocket日志實(shí)時(shí)輸出到web頁面

    spring?boot集成WebSocket日志實(shí)時(shí)輸出到web頁面

    這篇文章主要為大家介紹了spring?boot集成WebSocket日志實(shí)時(shí)輸出到web頁面展示的詳細(xì)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • 帶你了解Spring中bean的獲取

    帶你了解Spring中bean的獲取

    這篇文章主要介紹了Spring在代碼中獲取bean的幾種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 基于mybatis 動(dòng)態(tài)SQL查詢總結(jié)

    基于mybatis 動(dòng)態(tài)SQL查詢總結(jié)

    這篇文章主要介紹了mybatis 動(dòng)態(tài)SQL查詢總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java.lang.Void類的解析與使用詳解

    java.lang.Void類的解析與使用詳解

    這篇文章主要介紹了java.lang.Void類的解析與使用詳解,文中涉及到了java.lang.integer類的源碼,分場(chǎng)景給大家介紹的非常詳細(xì),給大家補(bǔ)充介紹java.lang.Void 與 void的比較及使用,需要的朋友可以參考下
    2017-12-12
  • 解決IDEA插件市場(chǎng)Plugins無法加載的問題

    解決IDEA插件市場(chǎng)Plugins無法加載的問題

    這篇文章主要介紹了解決IDEA插件市場(chǎng)Plugins無法加載的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • java實(shí)現(xiàn)打印正三角的方法

    java實(shí)現(xiàn)打印正三角的方法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)打印正三角的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • springBoot整合rabbitMQ的方法詳解

    springBoot整合rabbitMQ的方法詳解

    這篇文章主要介紹了springBoot整合rabbitMQ的方法詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04

最新評(píng)論