当前位置: 首页 > news >正文

fastjson2 打开 AutoType

1. 功能简介

FASTJSON支持AutoType功能,这个功能在序列化的JSON字符串中带上类型信息,在反序列化时,不需要传入类型,实现自动类型识别。

2. AutoType安全机制介绍

  • 必须显式打开才能使用。和fastjson 1.x不一样,fastjson 1.x为了兼容有一个白名单,在fastjson 2中,没有任何白名单,也不包括任何Exception类的白名单,必须显式打开才能使用。这可以保证缺省配置下是安全的。
  • 支持配置safeMode,在safeMode打开后,显式传入AutoType参数也不起作用
  • 显式打开不推荐,打开后会有反序列化风险,打开AutoType不应该在暴露在公网的场景下使用。建议参照本文中的第5点代替AutoType功能。

3. fastjson2如何正确的打开autoType的功能

正常情况下,出于安全考虑,我们默认是关闭autoType的能力的,但是可以通过构建AutoTypeBeforeHandler白名单的方式来打开,废话不多说,上代码

package com.example.es.fastjson2;import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import lombok.extern.slf4j.Slf4j;/*** @author peng.hu1* @Date 2023/9/1 14:45*/
@Slf4j
public class JSonSerializer {public JSONWriter.Feature[] features = new JSONWriter.Feature[]{JSONWriter.Feature.WriteClassName,JSONWriter.Feature.FieldBased,JSONWriter.Feature.ReferenceDetection,JSONWriter.Feature.NotWriteDefaultValue,JSONWriter.Feature.WriteNameAsSymbol,JSONWriter.Feature.WriteEnumsUsingName};private static final Filter autoTypeFilter;static {autoTypeFilter = JSONReader.autoTypeFilter(// 按需加上需要支持自动类型的类名前缀,范围越小越安全, 我这个就比较过分了,直接全部放开,哈哈"com.","org.","java.");}/*** 序列化* @param object 对象* @param classLoader* @return*/public byte[] serialize(Object object, ClassLoader classLoader) {ClassLoader swap = Thread.currentThread().getContextClassLoader();try {if (classLoader != null) {Thread.currentThread().setContextClassLoader(classLoader);}return JSON.toJSONBytes(object, features);} catch (Throwable t) {log.error("SerializeException" ,t);throw new RuntimeException("serialize error", t);} finally {if (classLoader != null) {Thread.currentThread().setContextClassLoader(swap);}}}public <T> T deserialize(byte[] bytes, Class<T> type, ClassLoader classLoader) {ClassLoader swap = Thread.currentThread().getContextClassLoader();try {if (classLoader != null) {Thread.currentThread().setContextClassLoader(classLoader);}try {return JSON.parseObject(bytes, type, autoTypeFilter,JSONReader.Feature.UseDefaultConstructorAsPossible,JSONReader.Feature.UseNativeObject,JSONReader.Feature.FieldBased);} catch (Exception e) {return JSON.parseObject(bytes, type);}} catch (Throwable t) {log.error("SerializeException" ,t);throw new RuntimeException("deserialize error", t);} finally {if (classLoader != null) {Thread.currentThread().setContextClassLoader(swap);}}}
}

这里面最核心的地方就在这里 JSONReader.autoTypeFilter(“*”), 这是个白名单过滤filter

http://www.lryc.cn/news/150231.html

相关文章:

  • 封装(个人学习笔记黑马学习)
  • PyTorch 模型性能分析和优化 - 第 3 部分
  • 【力扣每日一题】2023.9.1 买钢笔和铅笔的方案数
  • 实现不同局域网间的文件共享和端口映射,使用Python自带的HTTP服务
  • Kubernetes技术--k8s核心技术Pod
  • 基于Springboot实现的Echarts图表
  • adb server version (41) doesn‘t match this client (39)
  • B080-RabbitMQ
  • 关于岛屿的三道leetcode原题:岛屿周长、岛屿数量、统计子岛屿
  • lintcode 1081 · 贴纸拼单词【hard 递归+记忆化搜索才能通过】
  • HarmonyOS/OpenHarmony(Stage模型)应用开发单一手势(二)
  • 计算机毕设之基于Python+django+MySQL可视化的学习系统的设计与实现
  • Kotlin inline、noinline、crossinline 深入解析
  • 在 CentOS 7 / RHEL 7 上安装 Python 3.11
  • SVN基本使用笔记——广州云科
  • python爬虫-Selenium
  • flutter plugins插件【一】【FlutterJsonBeanFactory】
  • 系统中出现大量不可中断进程和僵尸进程(理论)
  • L1-012 计算指数(Python实现) 测试点全过
  • String、StringBuffer、StringBuilder的区别
  • .net基础概念
  • 电缆工厂 3D 可视化管控系统 | 智慧工厂
  • bazel高效使用和调优
  • 【实训项目】传道学习助手APP设计
  • 短信验证码服务
  • windows如何更改/禁用系统更新
  • Clion 使用ffmpeg 学习1 开发环境配置
  • 浏览器连不上 Flink WebUI 8081 端口
  • Doris集群安装部署(1.2.4.1 release)
  • 对HashMap的value做升序、降序