Jackson 支持

Spring Security 为持久化与 Spring Security 相关的类提供 Jackson 支持。这可以提高在使用分布式会话(例如会话复制、Spring Session 等)时序列化与 Spring Security 相关的类的性能。

要使用它,请将 SecurityJackson2Modules.getModules(ClassLoader) 注册到 ObjectMapper (jackson-databind)

  • Java

  • Kotlin

ObjectMapper mapper = new ObjectMapper();
ClassLoader loader = getClass().getClassLoader();
List<Module> modules = SecurityJackson2Modules.getModules(loader);
mapper.registerModules(modules);

// ... use ObjectMapper as normally ...
SecurityContext context = new SecurityContextImpl();
// ...
String json = mapper.writeValueAsString(context);
val mapper = ObjectMapper()
val loader = javaClass.classLoader
val modules: MutableList<Module> = SecurityJackson2Modules.getModules(loader)
mapper.registerModules(modules)

// ... use ObjectMapper as normally ...
val context: SecurityContext = SecurityContextImpl()
// ...
val json: String = mapper.writeValueAsString(context)

以下 Spring Security 模块提供 Jackson 支持

  • spring-security-core (CoreJackson2Module)

  • spring-security-web (WebJackson2Module, WebServletJackson2Module, WebServerJackson2Module)

  • spring-security-oauth2-client (OAuth2ClientJackson2Module)

  • spring-security-cas (CasJackson2Module)