解决:java.io.InvalidClassException: org.springframework.security.core.authority.SimpleGrantedAuthority

在做oauth认证的时候报错
java.io.InvalidClassException: org.springframework.security.core.authority.SimpleGrantedAuthority; local class incompatible: stream classdesc serialVersionUID = 500, local class serialVersionUID = 510
org.springframework.data.redis.serializer.SerializationException: Cannot serialize
csdn上找了一篇博客,说是:这是序列化异常,spring-security-core5.0.9中SimpleGrantedAuthority类serialVersionUID默认值是500,spring-security-core5.1.2中SimpleGrantedAuthority类serialVersionUID默认值是510。去pom.xml中修改版本号
博客地址:https://blog.csdn.net/smd2575624555/article/details/87080294
但是博主并没有说是改谁的版本号,就打开项目看了一下,果然我自己的项目用的org.springframework.security.core是5.1.5版本,他的serialVersionUID值真的是510,而授权模块的org.springframework.security.core是5.0.10版本,他的serialVersionUID值真的是500,那么问题基本确定就是版本不一致导致的。
接下来就是调整版本,因为不知道调整谁,所以一开始就乱调整,最终确定是spring boot的版本不一致导致的,我用的是2.1.6,而授权模块是2.0.7,于是将

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/>
</parent>

改为

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
</parent>
已标记关键词 清除标记
<div><p>I'm having this exception at runtime </p> <pre><code> javascript Caused by: java.io.InvalidClassException: org.springframework.security.core.authority.SimpleGrantedAuthority; local class incompatible: stream classdesc serialVersionUID = 320, local class serialVersionUID = 400 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at java.util.HashSet.readObject(HashSet.java:333) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at org.springframework.security.oauth2.common.util.SerializationUtils.deserialize(SerializationUtils.java:36) ... 139 more </code></pre> <p>I've seen in this <a href="http://stackoverflow.com/questions/10378855/java-io-invalidclassexception-local-class-incompatible">StackOverflow question</a> that this problem is fixed just by adding a generated serialVersionUID. Apparently, <a href="https://github.com/spring-projects/spring-security/blob/c8f598778f3d45e0092385d20593ade6f33e138f/core/src/main/java/org/springframework/security/core/authority/SimpleGrantedAuthority.java">SimpleGrantedAuthority</a> class doesn't have a generated one which may cause this problem. </p> <p>Is there any workaround for this?</p><p>该提问来源于开源项目:spring-projects/spring-security-oauth</p></div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页