Jackson サポート

Spring Security は、Spring Security 関連クラスを永続化するための Jackson サポートを提供します。これにより、分散セッション(つまり、セッションレプリケーション、Spring Session など)で作業する際に Spring Security 関連クラスを直列化するパフォーマンスを改善できます。

使用するには、SecurityJackson2Modules.getModules(ClassLoader) を ObjectMapper に登録します(jackson-databind [GitHub] (英語) ):

  • 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 (WebJackson2ModuleWebServletJackson2ModuleWebServerJackson2Module)

  • spring-security-oauth2-client (OAuth2ClientJackson2Module)

  • spring-security-cas (CasJackson2Module)