RedisTokenstore:无法将...用户转换为...用户

RedisTokenstore: Cannot cast ...User to ...User

我将 Spring OAuth2 应用切换到了 RedisTokenstore。现在访问主体,基本上是通过...

SecurityContextHolder.getContext().authentication.userAuthentication.principal as User

...我得到一个...

java.lang.ClassCastException: ...User cannot be cast to ...User

(三个点实际上是完整的 class 路径)。

我对 jave / kotlin 很陌生,对正在发生的事情只有模糊的了解。

一些提示:

更新: 串口版本hickhup是因为我添加串口后没有刷新redis缓存。所以这只是 cannot cast 错误。

如何开始调试?

spring-devtools 实际上有一个问题:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-known-restart-limitations

20.2.7 Known Limitations

Restart functionality does not work well with objects that are deserialized by using a standard ObjectInputStream. If you need to deserialize data, you may need to use Spring’s ConfigurableObjectInputStream in combination with Thread.currentThread().getContextClassLoader().

Unfortunately, several third-party libraries deserialize without considering the context classloader. If you find such a problem, you need to request a fix with the original authors.

一个选择是升级到spring-boot 2.+

另一种方法是将 devtools 全部删除,假设您不需要它。下面是一个示例,如果您使用 Maven,它将删除 1.3.0 及更高版本的任何版本:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>[1.3.0,)</version>
    <scope>provided</scope>
</dependency>

完成后一定要先刷新Redis进行验证。