OAuth2 [org.springframework.data.redis.serializer.SerializationException: Cannot serialize

不想看分析过称的 ,文章结尾就是解决方法

OAuth2 虽然公司项目用了,但只是浅尝辄止,今天自己搭了一下服务,出现这么一个问题。
我是用RedisTokenStore存储token refresh token,在redisTokenStore 存储的时候,会把要存储的对象序列化变成byte数组放到redis里面。

这个地方就出问题了。
具体的提示是:
Resolved [org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: org.springframework.dao.support.PersistenceExceptionTranslationInterceptor]

解决问题

debug了一下源码,发现是在序列化OAuth2Authentication的时候 有了问题,很奇怪,这里使用的JDK的序列化,没道理会出错的,
重点是: 我的另外一个项目序列化的时候 是没问题的!
仔细对比了一下,最终发现原因

原因

OAuth2Authentication会持有一个Authentication引用,这个可以从它的构造方法得知,这个Authentication是我们前面的filter chain里面认证对象时候产生的。Authentication的GrantedAuthority,是
UserDetails提供的。他们之间有引用的关系。

	/**
	 * Construct an OAuth 2 authentication. Since some grant types don't require user authentication, the user
	 * authentication may be null.
	 * 
	 * @param storedRequest The authorization request (must not be null).
	 * @param userAuthentication The user authentication (possibly null).
	 */
	public OAuth2Authentication(OAuth2Request storedRequest, Authentication userAuthentication) {
   
		super(userAuthentication == null ? storedRequest.getAuthorities() : userAuthentication.getAuthorities());
		this.storedRequest = storedRequest;
		this.userAuthentication = userAuthentication;
	}

我这个项目里面的UserDetails是我自己扩展的
代码如下

package com.example.sercurity.bo;

import com.example.sercurity.entity.Role;
import com.example.sercurity.entity.User;
import com.example.sercurity.service.PermissonService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority
`@EnableOAuth2Sso` 是Spring Security OAuth项目中的一个注解,用于启用OAuth2的单点登录(Single Sign On,简称SSO)功能。该注解一般用在Spring Boot应用中,以便简化OAuth2 SSO的配置。在使用该注解时,通常需要在项目中添加相应的依赖来支持OAuth2 SSO的实现。 在Maven项目中,除了需要添加Spring Boot的自动配置依赖之外,还需要添加专门支持OAuth2的依赖库,例如`spring-security-oauth2-client`和`spring-security-oauth2-resource-server`。以下是一个可能的依赖配置示例: ```xml <dependencies> <!-- Spring Boot Starter Parent, 提供了项目的基础依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>你的Spring Boot版本</version> </dependency> <!-- Spring Boot Starter Web, 包含了构建web应用的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Security OAuth2 Client --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security-oauth2-client</artifactId> </dependency> <!-- Spring Security OAuth2 Resource Server --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security-oauth2-resource-server</artifactId> </dependency> <!-- 其他依赖... --> </dependencies> ``` 请确保将上述示例中的版本号替换为当前可用的版本,并根据实际情况添加其他必要的依赖。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值