Skip to content

Commit 1d5dae8

Browse files
Merge branch '3.2.x' into 3.3.x
Closes gh-3015
2 parents bde31df + d5daa28 commit 1d5dae8

File tree

2 files changed

+18
-2
lines changed
  • spring-session-docs/modules/ROOT/pages/configuration
  • spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config

2 files changed

+18
-2
lines changed

spring-session-docs/modules/ROOT/pages/configuration/jdbc.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ You can create a new one if you prefer.
294294
You might need to do the same for other objects that are persisted in the session.
295295
<4> Add the `JsonSerializer`/`JsonDeserializer` that we created into the `ConversionService`.
296296

297-
Now that we configured how Spring Session JDBC converts our attributes values into `byte[]`, we must customize the query that insert the session attributes.
297+
Now that we configured how Spring Session JDBC converts our attributes values into `byte[]`, we must customize the query that inserts and updates the session attributes.
298298
The customization is necessary because Spring Session JDBC sets content as bytes in the SQL statement, however, `bytea` is not compatible with `jsonb`, therefore we need to encode the `bytea` value to text and then convert it to `jsonb`.
299299

300300
[tabs]
@@ -336,6 +336,12 @@ public class SessionConfig {
336336
And that's it, you should now be able to see the session attributes saved as JSON in the database.
337337
There is a https://github.com/spring-projects/spring-session/tree/main/spring-session-samples/spring-session-sample-boot-jdbc-json-attribute[sample available] where you can see the whole implementation and run the tests.
338338

339+
[NOTE]
340+
====
341+
If your https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/user-details.html#page-title[`UserDetails` implementation] extends Spring Security's `org.springframework.security.core.userdetails.User` class, it is important that you register a custom deserializer for it.
342+
Otherwise, Jackson will use the existing `org.springframework.security.jackson2.UserDeserializer` which won't result in the expected `UserDetails` implementation. See https://github.com/spring-projects/spring-session/issues/3009[gh-3009] for more details.
343+
====
344+
339345
[[specifying-datasource]]
340346
== Specifying an alternative `DataSource`
341347

spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/main/java/sample/config/SessionConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ public class SessionConfig implements BeanClassLoaderAware {
2626
VALUES (?, ?, encode(?, 'escape')::jsonb)
2727
""";
2828

29+
private static final String UPDATE_SESSION_ATTRIBUTE_QUERY = """
30+
UPDATE %TABLE_NAME%_ATTRIBUTES
31+
SET ATTRIBUTE_BYTES = encode(?, 'escape')::jsonb
32+
WHERE SESSION_PRIMARY_ID = ?
33+
AND ATTRIBUTE_NAME = ?
34+
""";
35+
2936
private ClassLoader classLoader;
3037

3138
@Bean
3239
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> customizer() {
33-
return (sessionRepository) -> sessionRepository.setCreateSessionAttributeQuery(CREATE_SESSION_ATTRIBUTE_QUERY);
40+
return (sessionRepository) -> {
41+
sessionRepository.setCreateSessionAttributeQuery(CREATE_SESSION_ATTRIBUTE_QUERY);
42+
sessionRepository.setUpdateSessionAttributeQuery(UPDATE_SESSION_ATTRIBUTE_QUERY);
43+
};
3444
}
3545

3646
@Bean("springSessionConversionService")

0 commit comments

Comments
 (0)