Reputation: 527
I don't understand why this filter, the rewrite function is not executing. The re-write function does not transform anything, but should just log out a simple message.
override fun apply(config: Config): GatewayFilter = GatewayFilter { exchange, chain ->
// only modify /user-info endpoint responses
if (exchange.request.path.value() != "${serverProperties.resourceServerPrefix}/user-info") {
logger.debug("request did not match userinfo endpoint")
chain.filter(exchange)
} else {
chain.filter(exchange).then(
exchange.session.flatMap { session ->
logger.debug("request did match userinfo endpoint")
val sessionId = session.id
val sessionKey = "${sessionProperties.redis?.sessionNamespace}:sessions:$sessionId"
redisTemplate.opsForHash<String, Any>()
.get(sessionKey, "creationTime")
.doOnNext { creationTime ->
logger.debug("3. Found creation time in Redis: {}", creationTime)
}
.flatMap { creationTime ->
val created = Instant.ofEpochMilli(creationTime.toString().toLong())
logger.debug("4. Converted to Instant: {}", created)
// Define the RewriteFunction separately
val rewriteFunction = RewriteFunction<String, String> { _, originalResponse ->
logger.debug("5. Original response as string: {}", originalResponse)
Mono.just(originalResponse) // Return the original response as is
}
val modifyConfig = ModifyResponseBodyGatewayFilterFactory.Config().apply {
logger.debug("Setting up rewrite function")
setInClass(String::class.java)
setOutClass(String::class.java)
setRewriteFunction(String::class.java, String::class.java, rewriteFunction)
}
val modifyFilter = modifyResponseBodyFilterFactory.apply(modifyConfig)
// Apply modification filter first, then the chain
modifyFilter.filter(exchange, chain)
.doOnSubscribe { logger.debug("Subscribed to response modification filter") }
.doOnSuccess { logger.debug("Modified response completed") }
.doOnError { error -> logger.error("Modification failed", error) }
}
.onErrorResume { error ->
// log the Redis error but allow the chain to continue
logger.error("Failed to get session timing from Redis", error)
Mono.empty()
}
}
)
}
}
The only things that get logged are:
2025-01-15T20:13:33.074Z DEBUG 32653 --- [BFFApplication] [ctor-http-nio-4] c.v.b.r.s.SessionInfoResponseFilter : request did match userinfo endpoint
2025-01-15T20:13:33.091Z DEBUG 32653 --- [BFFApplication] [xecutorLoop-3-4] c.v.b.r.s.SessionInfoResponseFilter : 3. Found creation time in Redis: 1736971305338
2025-01-15T20:13:33.091Z DEBUG 32653 --- [BFFApplication] [xecutorLoop-3-4] c.v.b.r.s.SessionInfoResponseFilter : 4. Converted to Instant: 2025-01-15T20:01:45.338Z
2025-01-15T20:13:33.092Z DEBUG 32653 --- [BFFApplication] [xecutorLoop-3-4] c.v.b.r.s.SessionInfoResponseFilter : Setting up rewrite function
2025-01-15T20:13:33.096Z DEBUG 32653 --- [BFFApplication] [xecutorLoop-3-4] c.v.b.r.s.SessionInfoResponseFilter : Subscribed to response modification filter
2025-01-15T20:13:33.096Z DEBUG 32653 --- [BFFApplication] [xecutorLoop-3-4] c.v.b.r.s.SessionInfoResponseFilter : Modified response completed
What can I try next? I can't see where I've gone wrong.
Upvotes: 0
Views: 28