-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: Bust OAuth2 client cache for spring boot 3.3 #36660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.appsmith.server.migrations.db.ce; | ||
|
|
||
| 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; | ||
|
|
||
| @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); | ||
| } | ||
|
Comment on lines
+23
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add exception handling in the execute method It's good practice to handle exceptions to make your code more robust. In the public void execute() {
try {
doClearRedisOAuth2AuthClientKeys(reactiveRedisOperations);
} catch (Exception e) {
log.error("An error occurred while clearing OAuth2 client keys from Redis", e);
// Optionally, handle the exception or rethrow it
}
} |
||
|
|
||
| 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"; | ||
|
Comment on lines
+31
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using KEYS command in Redis scripts Remember that using the Instead, consider using the final String script =
"local cursor = '0' " +
"repeat " +
" local result = redis.call('SCAN', cursor, 'MATCH', 'spring:session:sessions:*') " +
" cursor = result[1] " +
" local keys = result[2] " +
" for _,k in ipairs(keys) do " +
" local fieldExists = redis.call('hexists', k, '" + authorizedClientsKey + "') " +
" if fieldExists == 1 then " +
" redis.call('del', k) " +
" end " +
" end " +
"until cursor == '0'"; |
||
| final Flux<Object> flushdb = reactiveRedisOperations.execute(RedisScript.of(script)); | ||
|
|
||
| flushdb.blockLast(); | ||
| } | ||
|
Comment on lines
+27
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid blocking calls in reactive streams Dear student, it's important to remember that using blocking calls like To maintain a non-blocking, asynchronous flow, consider refactoring the code to use reactive operators and subscribing to the sequence without blocking. Here's how you might modify the code: reactiveRedisOperations.execute(RedisScript.of(script))
.doOnError(error -> log.error("Error executing Redis script", error))
.doOnComplete(() -> log.info("Successfully cleared OAuth2 client keys from Redis"))
.subscribe(); |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.