Skip to content

Commit

Permalink
fix: Clear all session for Spring upgrade (#36695)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-nair authored Oct 7, 2024
1 parent b447e6a commit 67c9ae2
Showing 1 changed file with 10 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@
import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import io.mongock.api.annotations.RollbackExecution;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.script.RedisScript;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ReactiveRedisTemplate;

@RequiredArgsConstructor
@Slf4j
@ChangeUnit(order = "063", id = "reset_session_oauth2_spring_3_3")
public class Migration063CacheBustSpringBoot3_3 {

private final ReactiveRedisOperations<String, String> reactiveRedisOperations;

@RollbackExecution
public void rollbackExecution() {}

@Execution
public void execute() {
doClearRedisOAuth2AuthClientKeys(reactiveRedisOperations);
}

public static void doClearRedisOAuth2AuthClientKeys(
ReactiveRedisOperations<String, String> reactiveRedisOperations) {
final String authorizedClientsKey =
"sessionAttr:org.springframework.security.oauth2.client.web.server.WebSessionServerOAuth2AuthorizedClientRepository.AUTHORIZED_CLIENTS";
final String script =
"for _,k in ipairs(redis.call('keys','spring:session:sessions:*')) do local fieldExists = redis.call('hexists', k, '"
+ authorizedClientsKey + "'); if fieldExists == 1 then redis.call('del', k) end end";
final Flux<Object> flushdb = reactiveRedisOperations.execute(RedisScript.of(script));

flushdb.blockLast();
public void execute(
@Qualifier("reactiveRedisTemplate") final ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
reactiveRedisTemplate
.getConnectionFactory()
.getReactiveConnection()
.serverCommands()
.flushDb()
.block();
}
}

0 comments on commit 67c9ae2

Please sign in to comment.