'How to customize SessionRepository in Spring Boot Redis

SafeDeserializationRepository is a class that I extended from SessionRepository. I'd like Redis to use this class rather than its SessionRepository. How can I implement it?

class SafeDeserializationRepository<S : Session?>(
    private val delegate: SessionRepository<S>,
    private val redisTemplate: RedisTemplate<String, String>
) : SessionRepository<S> {

    override fun createSession(): S {
        return delegate.createSession()
    }

    override fun save(session: S) {
        delegate.save(session)
    }

    override fun findById(p0: String?): S? {
        return try {
            delegate.findById(p0)
        } catch (e: Exception) {
            println("Deleting non-deserializable session with key $p0")
            redisTemplate.delete(BOUNDED_HASH_KEY_PREFIX + p0)
            null
        }
    }

    override fun deleteById(p0: String?) {
        delegate.deleteById(p0)
    }

    companion object {
        private const val BOUNDED_HASH_KEY_PREFIX = "spring:session:sessions:"
    }
}


Solution 1:[1]

Just need to annotate the class as a component and primary.

@Primary
@Component
class SafeDeserializationRepository<S : Session?>(
    private val delegate: SessionRepository<S>,
    private val redisTemplate: RedisTemplate<String, String>
) : SessionRepository<S> {

    ....

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Behrooz Fard