|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Security 7.0.4! |
Jackson 支持
Spring Security 提供了对 Jackson 3 的支持,用于持久化 Spring Security 相关的类。 在使用分布式会话(例如会话复制、Spring Session 等)时,这可以提升序列化 Spring Security 相关类的性能。
|
Jackson 2 的支持仍然可用,但已被标记为弃用并将在未来移除,因此建议您迁移到 Jackson 3。 |
要使用它,请将 SecurityJacksonModules.getModules(ClassLoader) 注册到 JsonMapper.Builder(jackson-databind)中:
-
Java
-
Kotlin
ClassLoader loader = getClass().getClassLoader();
JsonMapper mapper = JsonMapper.builder()
.addModules(SecurityJacksonModules.getModules(loader))
.build();
// ... use JsonMapper as normally ...
SecurityContext context = new SecurityContextImpl();
// ...
String json = mapper.writeValueAsString(context);
val loader = javaClass.classLoader
val mapper = JsonMapper.builder()
.addModules(SecurityJacksonModules.getModules(loader))
.build()
// ... use JsonMapper as normally ...
val context: SecurityContext = SecurityContextImpl()
// ...
val json: String = mapper.writeValueAsString(context)
|
如上所示使用 |
如有需要,您可以向验证处理中添加自定义类。
-
Java
-
Kotlin
ClassLoader loader = getClass().getClassLoader();
BasicPolymorphicTypeValidator.Builder builder = BasicPolymorphicTypeValidator.builder()
.allowIfSubType(MyCustomType.class);
JsonMapper mapper = JsonMapper.builder()
.addModules(SecurityJacksonModules.getModules(loader, builder))
.build();
val loader = javaClass.classLoader
val builder = BasicPolymorphicTypeValidator.builder()
.allowIfSubType(MyCustomType::class)
val mapper = JsonMapper.builder()
.addModules(SecurityJacksonModules.getModules(loader, builder))
.build()
|
以下 Spring Security 模块提供了对 Jackson 的支持:
|