ReactiveRedisTemplate opsForValue().set not working

Viewed 32

I am trying to save oauth login credentials in redis . Using spring oauth2 library, and spring redis.

My saveAuthorizedClient method should be saving the key/val pair in redis, but nothing is being saved. Have attached a debugger and the key value pairs are correctly assigned, its the reactiveOauthRedisTemplate.opsForValue().set(key, authorizedClient) which seems to be not doing anything.


class CustomClientService(
        val reactiveClientRegistrationRepository: ReactiveClientRegistrationRepository,
        val reactiveOauthRedisTemplate: ReactiveRedisTemplate<String, OAuth2AuthorizedClient>
) : ReactiveOAuth2AuthorizedClientService {


    override fun <T : OAuth2AuthorizedClient> loadAuthorizedClient(clientRegistrationId: String, principalName: String): Mono<T> {
        Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty")
        Assert.hasText(principalName, "principalName cannot be empty")
        return clientRegistrationRepository.findByRegistrationId(clientRegistrationId)
                .map { rediskey(clientRegistrationId, principalName) }
                .flatMap { key: String ->
                    reactiveOauthRedisTemplate.opsForValue().get(key) as Mono<T>
                }
    }

    override fun saveAuthorizedClient(authorizedClient: OAuth2AuthorizedClient, principal: Authentication): Mono<Void> {
        val key = rediskey(authorizedClient.clientRegistration.registrationId, principal.name)
        reactiveOauthRedisTemplate.opsForValue().set(key, authorizedClient)
                .let {
                    val x = reactiveOauthRedisTemplate.opsForValue().get(key)
                    println("my redis data is $x")
                    x.map {
                        println("my data value is $it")
                        val token = x as OAuth2AuthorizedClient
                    }
                }

        return Mono.empty()
    }

    private fun rediskey(clientRegistrationId: String, principalName: String): String {
        return "something..."
    }
}

Here's my redis config

@Configuration
class RedisConfig {
    @Bean
    fun reactiveOauthRedisTemplate(reactiveRedisConnectionFactory: ReactiveRedisConnectionFactory,
                                   resourceLoader: ResourceLoader): ReactiveRedisTemplate<String, OAuth2AuthorizedClient> {
        val keySerializer: RedisSerializer<String> = StringRedisSerializer()
        val defaultSerializer = JdkSerializationRedisSerializer(resourceLoader.classLoader)
        val serializationContext = RedisSerializationContext
                .newSerializationContext<String, OAuth2AuthorizedClient>(defaultSerializer).key(keySerializer).hashKey(keySerializer)
                .build()
        return ReactiveRedisTemplate(reactiveRedisConnectionFactory, serializationContext)
    }
}

redis yaml config

spring:
  redis:
    host: localhost

From debugger , the redis connect has host and port correct. Any help here is appreciated.

Here's my redis client object from debugger

redisURI = {RedisURI@12018} "redis://127.0.0.1"
 host = "127.0.0.1"
 socket = null
 sentinelMasterId = null
 port = 6379
 database = 0
 clientName = null
 username = null
 password = null
 ssl = false
 verifyMode = {SslVerifyMode@12033} "FULL"
 startTls = false
 timeout = {Duration@12010} "PT1M"
 sentinels = {ArrayList@12034}  size = 0
1 Answers

Your usage of react client is not correct, you should not return Mono.empty() from saveAuthorizedClient, you should return Mono<Boolean> and subscribe to it. You can ignore the return value if you do not need the result, but you must subscribe it so that it will execute the business logic.

Related