Merge branch '3.0.x'

Closes gh-2307
This commit is contained in:
Marcus Da Coregio
2023-06-06 15:21:49 -03:00
2 changed files with 39 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import io.lettuce.core.RedisCommandExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -27,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.DefaultMessage;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -45,8 +47,13 @@ import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
/**
* Integration tests for {@link RedisIndexedSessionRepository}.
@@ -682,6 +689,33 @@ class RedisIndexedSessionRepositoryITests extends AbstractRedisITests {
assertThat(this.repository.findById(copy2.getId())).isNull();
}
// gh-1743
@Test
@SuppressWarnings("unchecked")
void saveChangeSessionIdWhenFailedRenameOperationExceptionContainsMoreDetailsThenIgnoreError() {
RedisOperations<Object, Object> sessionRedisOperations = (RedisOperations<Object, Object>) ReflectionTestUtils
.getField(this.repository, "sessionRedisOperations");
RedisOperations<Object, Object> spyOperations = spy(sessionRedisOperations);
ReflectionTestUtils.setField(this.repository, "sessionRedisOperations", spyOperations);
RedisSession toSave = this.repository.createSession();
String sessionId = toSave.getId();
this.repository.save(toSave);
RedisIndexedSessionRepository.RedisSession session = this.repository.findById(sessionId);
this.repository.deleteById(sessionId);
String newSessionId = session.changeSessionId();
RedisSystemException redisSystemException = new RedisSystemException(null,
new RedisCommandExecutionException("ERR no such key. channel: [id: 0xec125091,..."));
willThrow(redisSystemException).given(spyOperations).rename(any(), any());
this.repository.save(session);
assertThat(this.repository.findById(sessionId)).isNull();
assertThat(this.repository.findById(newSessionId)).isNull();
reset(spyOperations);
}
private String getSecurityName() {
return this.context.getAuthentication().getName();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@ import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* <p>
@@ -913,7 +914,8 @@ public class RedisIndexedSessionRepository
}
private void handleErrNoSuchKeyError(NonTransientDataAccessException ex) {
if (!"ERR no such key".equals(NestedExceptionUtils.getMostSpecificCause(ex).getMessage())) {
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
if (!StringUtils.startsWithIgnoreCase(message, "ERR no such key")) {
throw ex;
}
}