Is someone able to help me on where i’m going wrong
I keep getting this error, with the request repository, that I’m trying to store in redis
The error I keep getting.
Error
java.lang.ClassCastException: class java.lang.String cannot be cast to class org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest (java.lang.String is in module java.base of loader 'bootstrap'; org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest is in unnamed module of loader 'app')
Redis serialiser
@Bean
// setting a custom OAuth2AuthorizationRequest serializer for Redis
fun oauth2AuthorizationRequestRedisSerializer(objectMapper: ObjectMapper): RedisSerializer<OAuth2AuthorizationRequest> {
return object : RedisSerializer<OAuth2AuthorizationRequest> {
private val serializer = GenericJackson2JsonRedisSerializer(objectMapper)
override fun serialize(value: OAuth2AuthorizationRequest?): ByteArray? {
return try {
if (value == null) {
null
} else {
println("Serializing: $value")
serializer.serialize(value)
}
} catch (e: Exception) {
throw SerializationException("Error serializing OAuth2AuthorizationRequest", e)
}
}
override fun deserialize(bytes: ByteArray?): OAuth2AuthorizationRequest? {
return try {
if (bytes == null || bytes.isEmpty()) {
println("Deserialization: Received null or empty byte array")
null
} else {
println("Deserializing bytes: ${bytes.joinToString(", ") { String.format("%02X", it) }}")
val result = serializer.deserialize(bytes)
println("Deserialized OAuth2AuthorizationRequest: $result")
result as? OAuth2AuthorizationRequest
}
} catch (e: Exception) {
throw SerializationException("Error deserializing OAuth2AuthorizationRequest", e)
}
}
}
}
private fun objectMapper(): ObjectMapper {
val mapper = ObjectMapper()
// register modules for security if needed
mapper.registerModule(CoreJackson2Module())
mapper.registerModules(SecurityJackson2Modules.getModules(loader))
return mapper
}
Repository implementation
@Repository
internal class RedisAuthorizationRequestRepository(
private val redisTemplate: ReactiveRedisTemplate<String, OAuth2AuthorizationRequest>,
springSessionProperties: SpringSessionProperties,
private val objectMapper: ObjectMapper
) : ServerAuthorizationRequestRepository<OAuth2AuthorizationRequest> {
private val redisKeyPrefix = springSessionProperties.redis?.oauth2RequestNamespace
override fun saveAuthorizationRequest(
authorizationRequest: OAuth2AuthorizationRequest?,
exchange: ServerWebExchange
): Mono<Void> {
return constructRedisKey(exchange).flatMap { redisKey ->
if (authorizationRequest != null) {
val hashOperations = redisTemplate.opsForHash<String, Any>()
val fieldsMap = objectMapper.convertValue(
authorizationRequest,
object : TypeReference<Map<String, Any>>() {}
)
hashOperations.putAll(redisKey, fieldsMap).then()
} else {
redisTemplate.opsForHash<String, Any>().delete(redisKey).then()
}
}
}
override fun loadAuthorizationRequest(exchange: ServerWebExchange): Mono<OAuth2AuthorizationRequest?> {
return constructRedisKey(exchange).flatMap { redisKey ->
redisTemplate.opsForHash<String, Any>().entries(redisKey)
.collectMap({ it.key as String }, { it.value })
.mapNotNull { map ->
if (map.isEmpty()) {
null
} else {
objectMapper.convertValue(map, OAuth2AuthorizationRequest::class.java)
}
}
}
}
}
It keeps saying string cannot be cast to OAuth2AuthorizationRequest …but cannot pinpoint the root cause