2021-11-10 15:38:29 -07:00
[[jackson]]
= Jackson Support
2021-04-21 16:01:26 -05:00
Spring Security provides Jackson support for persisting Spring Security-related classes.
This can improve the performance of serializing Spring Security-related classes when working with distributed sessions (session replication, Spring Session, and so on).
2021-11-10 15:38:29 -07:00
2025-09-01 18:23:31 +02:00
To use it, register the `SecurityJacksonModules.getModules(ClassLoader)` with `JsonMapper.Builder` (https://github.com/FasterXML/jackson-databind[jackson-databind]):
2021-11-10 15:38:29 -07:00
[source,java]
----
ClassLoader loader = getClass().getClassLoader();
2025-09-01 18:23:31 +02:00
JsonMapper mapper = JsonMapper.builder()
.addModules(SecurityJacksonModules.getModules(loader))
.build();
2021-11-10 15:38:29 -07:00
2025-09-01 18:23:31 +02:00
// ... use JsonMapper as normally ...
2021-11-10 15:38:29 -07:00
SecurityContext context = new SecurityContextImpl();
// ...
String json = mapper.writeValueAsString(context);
----
[NOTE]
====
The following Spring Security modules provide Jackson support:
2025-09-01 18:23:31 +02:00
- spring-security-core (javadoc:org.springframework.security.jackson.CoreJacksonModule[])
- spring-security-web (javadoc:org.springframework.security.web.jackson.WebJacksonModule[], javadoc:org.springframework.security.web.jackson.WebServletJacksonModule[], javadoc:org.springframework.security.web.server.jackson.WebServerJacksonModule[])
- <<oauth2client, spring-security-oauth2-client>> (javadoc:org.springframework.security.oauth2.client.jackson.OAuth2ClientJacksonModule[])
- spring-security-cas (javadoc:org.springframework.security.cas.jackson.CasJacksonModule[])
2021-11-10 15:38:29 -07:00
====