Skip to content

Spring Session and Dev Tools Cause ClassCastException #3805

@rwinch

Description

@rwinch

If Spring Session Redis and Spring Boot Dev Tools are being used and a user defined class is persisted into HttpSession a ClassCastException will occur when trying to load it.

The problem is that Spring Session uses Spring Data Redis to load the class on each request. Spring Data Redis eventually uses DefaultDeserializer which uses an ObjectInputStream to load the class from the system ClassLoader.

This means that the value restored from session has a class defined by the system ClassLoader. The application itself loads the same class from Spring Boot's RestartClassLoader. Since the two classes are loaded from different ClassLoaders they cannot be cast from one to the other.

One possible solution is for Spring Redis to use a mechanism to customize the ObjectInputStream ClassLoader to use the ThreadLocal.getThread().getContextClassLoader(). For example, it could use ConfigurableObjectInputStream

I believe one alternative is that if DefaultDeserializer were loaded using RestartClassLoader, then the ObjectInputStream should use the RestartClassLoader.

Related (possibly replaces this issue): SPR-13409

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions