diff --git a/src/main/java/com/example/sinitto/auth/controller/AuthController.java b/src/main/java/com/example/sinitto/auth/controller/AuthController.java index 009c3b9e..fb11918d 100644 --- a/src/main/java/com/example/sinitto/auth/controller/AuthController.java +++ b/src/main/java/com/example/sinitto/auth/controller/AuthController.java @@ -56,4 +56,10 @@ public ResponseEntity kakaoCallback(@RequestParam("code") String return ResponseEntity.ok().body(loginResponse); } + @Operation(summary = "Redis안의 모든 데이터 제거", description = "발급된 refreshToken을 사용하지 못하게 Redis 안의 모든 데이터를 제거합니다.") + @DeleteMapping("/redis") + public ResponseEntity deleteAllDataFromRedis(){ + tokenService.deleteAllDataFromRedis(); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/src/main/java/com/example/sinitto/auth/service/TokenService.java b/src/main/java/com/example/sinitto/auth/service/TokenService.java index db57caa0..fa972d3d 100644 --- a/src/main/java/com/example/sinitto/auth/service/TokenService.java +++ b/src/main/java/com/example/sinitto/auth/service/TokenService.java @@ -119,4 +119,11 @@ public TokenResponse refreshAccessToken(String refreshToken) { return new TokenResponse(newAccessToken, newRefreshToken); } + + public void deleteAllDataFromRedis(){ + redisTemplate.getConnectionFactory() + .getConnection() + .serverCommands() + .flushAll(); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ed2a248b..f0993146 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,9 +4,6 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.show-sql=true logging.level.org.hibernate.orm.jdbc.bind=TRACE spring.profiles.active=dev -spring.data.redis.host=localhost -spring.data.redis.port=6379 -spring.data.redis.password=sinitto spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.h2.console.enabled=false diff --git a/src/test/java/com/example/sinitto/auth/service/TokenServiceTest.java b/src/test/java/com/example/sinitto/auth/service/TokenServiceTest.java index dca27884..d14be2ff 100644 --- a/src/test/java/com/example/sinitto/auth/service/TokenServiceTest.java +++ b/src/test/java/com/example/sinitto/auth/service/TokenServiceTest.java @@ -6,6 +6,9 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.jupiter.MockitoSettings; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; @@ -23,6 +26,15 @@ public class TokenServiceTest { @Mock private HashOperations hashOperations; + @Mock + private RedisConnectionFactory redisConnectionFactory; + + @Mock + private RedisConnection redisConnection; + + @Mock + private RedisServerCommands redisServerCommands; + private TokenService tokenService; @BeforeEach @@ -65,4 +77,31 @@ void generateRefreshTokenTest() { assertTrue(token.startsWith("ey")); assertEquals(email, resultEmail); } + + @Test + @DisplayName("deleteAllDataFromRedis 메소드 테스트") + void deleteAllDataFromRedisTest(){ + //given + String email = "test@email.com"; + String refreshToken = "test.Refresh.Token"; + + when(redisTemplate.opsForHash()).thenReturn(hashOperations); + when(redisTemplate.getConnectionFactory()).thenReturn(redisConnectionFactory); + when(redisConnectionFactory.getConnection()).thenReturn(redisConnection); + when(redisConnection.serverCommands()).thenReturn(redisServerCommands); + + redisTemplate.opsForHash().put(email, "refreshToken1", refreshToken); + redisTemplate.opsForHash().put(email, "refreshToken2", refreshToken); + redisTemplate.opsForHash().put(email, "refreshToken3", refreshToken); + redisTemplate.opsForHash().put(email, "refreshToken4", refreshToken); + + //when + tokenService.deleteAllDataFromRedis(); + + //then + assertNull(redisTemplate.opsForHash().get(email, "refreshToken1")); + assertNull(redisTemplate.opsForHash().get(email, "refreshToken2")); + assertNull(redisTemplate.opsForHash().get(email, "refreshToken3")); + assertNull(redisTemplate.opsForHash().get(email, "refreshToken4")); + } }