Skip to content

Commit

Permalink
Merge pull request #34 from SafeNet-2024/feature/add-security
Browse files Browse the repository at this point in the history
[feat] ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ƒ์„ธํ™” ๋ฐ Redis ๋งŒ๋ฃŒ ์‹œ๊ฐ„ 24์‹œ๊ฐ„ ์„ค์ • ์ถ”๊ฐ€
  • Loading branch information
Yeon-chae authored Jun 13, 2024
2 parents 42e1b63 + 918d8f8 commit 3f445b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; // ๋กœ๊น…์„ ์œ„ํ•ด ์ถ”๊ฐ€
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -59,10 +60,16 @@ public void message(MessageDto messageDto) {
@ApiResponse(responseCode = "500", description = "์„œ๋ฒ„ ์˜ค๋ฅ˜", content = @Content(mediaType = "application/json"))
})
@Parameter(name = "roomId", description = "๋ฉ”์‹œ์ง€๋ฅผ ์กฐํšŒํ•  ์ฑ„ํŒ…๋ฐฉ์˜ ID", required = true, example = "123")
public ResponseEntity<List<MessageDto>> loadMessage(
public ResponseEntity<?> loadMessage(
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@PathVariable String roomId) {
return ResponseEntity.ok(messageService.loadMessage(roomId));
try {
List<MessageDto> messages = messageService.loadMessage(roomId);
return ResponseEntity.ok(messages);
} catch (Exception e) {
log.error("Failed to load messages for room {}: {}", roomId, e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to load messages: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MessageService(
this.messageRepository = messageRepository;
this.messageRoomRepository = messageRoomRepository;
}

public void saveMessage(MessageDto messageDto) {
MessageRoom messageRoom = messageRoomRepository.findByRoomId(messageDto.getRoomId())
.orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์ชฝ์ง€๋ฐฉ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
Expand All @@ -49,7 +50,7 @@ public void saveMessage(MessageDto messageDto) {

redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(MessageDto.class));
redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto);
redisTemplateMessage.expire(messageDto.getRoomId(), 1, TimeUnit.HOURS); // redis์—์„œ 1์‹œ๊ฐ„๋งˆ๋‹ค ์‚ญ์ œ
redisTemplateMessage.expire(messageDto.getRoomId(), 24, TimeUnit.HOURS); // redis์—์„œ 24์‹œ๊ฐ„๋งˆ๋‹ค ์‚ญ์ œ
}

/**
Expand Down Expand Up @@ -78,7 +79,8 @@ public List<MessageDto> loadMessage(String roomId) {
.build();
messageList.add(messageDto);
redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(Message.class)); // ์ง๋ ฌํ™”
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis ์ €์žฅ
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis ์ €์žฅ
redisTemplateMessage.expire(roomId, 24, TimeUnit.HOURS); // redis์—์„œ 24์‹œ๊ฐ„๋งˆ๋‹ค ์‚ญ์ œ
}
} else { // 4. ๋’ค์ชฝ์— ๋ฐ์ดํ„ฐ ๋ถ™์ด๊ธฐ
messageList.addAll(redisMessageList);
Expand Down

0 comments on commit 3f445b2

Please sign in to comment.