【三十三】springboot+序列化实现返回值脱敏和返回值字符串时间格式化问题

发布于:2024-06-12 ⋅ 阅读:(41) ⋅ 点赞:(0)

互相交流入口地址

整体目录:

【一】springboot整合swagger

【二】springboot整合自定义swagger

【三】springboot整合token

【四】springboot整合mybatis-plus

【五】springboot整合mybatis-plus

【六】springboot整合redis

【七】springboot整合AOP实现日志操作

【八】springboot整合定时任务

【九】springboot整合redis实现启动服务时热点数据保存在全局和缓存

【十】springboot整合quartz实现定时任务优化

【十一】springboot整合异步调用并获取返回值

【十二】springboot整合WebService

【十三】springboot整合WebService关于传参数

【十四】springboot整合WebSocket

【十五】springboot整合WebSocket实现聊天室

【十六】RabbitMQ基础篇(下载安装并基础使用,内含各种坑问题)

【十七】RabbitMQ基础篇(延迟队列和死信队列实战)

【十八】springboot实现自定义全局异常处理

【十九】初学Kafka并实战整合SpringCloudStream进行使用

【二十】springboot整合ElasticSearch实战(万字篇)

【二十一】springboot整合过滤器实战

【二十二】springboot整合拦截器实战并对比过滤器

【二十三】springboot整合activiti7(1)实战演示篇

【二十四】springboot整合spring事务详解以及实战

【二十五】springboot使用EasyExcel和线程池实现多线程导入Excel数据

【二十六】springboot整合jedis和redisson布隆过滤器处理缓存穿透

【二十七】springboot实现多线程事务处理

【二十八】springboot之threadLocal参数解析器实现session一样保存当前登录功能 

【二十九】springboot整合logback实现日志管理

【三十】springboot项目上高并发解决示例

【三十一】springboot+easyExcel实现多文件导出压缩包

目录

一、返回值脱敏

二、返回值日期格式化 


        很久没有写小作文了,赶着学子们参加考试的时间,继续记录点小东西,1、返回对象的字符串数据脱敏 ;2、返回对象针对字符串格式的时间的格式化。

一、返回值脱敏

        1、准备返回值对象

       2、准备接口

        3、准备脱敏注解

        4、准备序列化处理类
public class SensitiveInfoSerialize extends JsonSerializer<String> implements ContextualSerializer {

    private DesensitizationType type;

    public SensitiveInfoSerialize() {
    }

    public SensitiveInfoSerialize(final DesensitizationType type) {
        this.type = type;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        switch (this.type) {
            case ID_CARD:
                value = DesensitizedUtil.idCardNum(value, 4, 2);
                break;
            case MOBILE_PHONE: {
                value = DesensitizedUtil.mobilePhone(value);
                break;
            }
            default:
                break;
        }
        gen.writeString(value);
    }

    /**
     * 序列化时获取字段注解属性
     * @param serializerProvider
     * @param property
     * @return
     * @throws JsonMappingException
     */
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
        if (property != null) {
            // 此demo只处理String类型字段
            if (Objects.equals(property.getType().getRawClass(), String.class)) {
                SensitiveInfo sensitiveInfo = property.getAnnotation(SensitiveInfo.class);
                if (sensitiveInfo == null) {
                    sensitiveInfo = property.getContextAnnotation(SensitiveInfo.class);
                }
                if (sensitiveInfo != null) {
                    return new SensitiveInfoSerialize(sensitiveInfo.value());
                }
            }
            return serializerProvider.findValueSerializer(property.getType(), property);
        }
        return serializerProvider.findNullValueSerializer(null);
    }

}

        实现ContextualSerializer接口后重写的JsonSerializer方法就是为了找到需要处理的属性,而集成JsonSerializer后重写的serialize方法就是为了处理需要处理的属性。DesensitizedUtil是糊涂的工具。就这样就可以了。

        5、演示原本效果

        6、增加注解后效果

二、返回值日期格式化 

        在开发时返回值里的时间一定不只是Date、LocalDateTime、LocalDate,有时候也可能是字符串格式。此时常用的@JsonFormat注解就失去用武之地了,使用上面的方式也可以处理这种情况,下面进行展示。

        1、返回值增加时间字段

        2、原有效果

        3、使用常用的@JsonFormat注解进行处理

        处理字符串的时间以外,其他的时间都能正常处理,下面通过序列化的方式进行处理该字段。

         4、增加字符串日期格式处理注解

        5、准备序列化处理类
public class StringToDateSerialize extends JsonSerializer<String> implements ContextualSerializer {

    private String sourceFormat;

    private String targetFormat;

    public StringToDateSerialize() {
    }

    public StringToDateSerialize(final String sourceFormat, final String targetFormat) {
        this.sourceFormat = sourceFormat;
        this.targetFormat = targetFormat;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(DateUtil.format(DateUtil.parse(value,sourceFormat), targetFormat));
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
        if (property != null) {
            if (Objects.equals(property.getType().getRawClass(), String.class)) {
                StringToDate stringToDate = property.getAnnotation(StringToDate.class);
                if (stringToDate == null) {
                    stringToDate = property.getContextAnnotation(StringToDate.class);
                }
                if (stringToDate != null) {
                    return new StringToDateSerialize(stringToDate.source(),stringToDate.target());
                }
            }
            return serializerProvider.findValueSerializer(property.getType(), property);
        }
        return serializerProvider.findNullValueSerializer(null);
    }

}
        6、测试效果

        可看到字符串格式的时间,包括含有T的时间格式都能够成功处理。

        欢迎大佬们评论区讨论。 


网站公告

今日签到

点亮在社区的每一天
去签到