Skip to content

Commit

Permalink
refactor: 방, filter, aop 수정 (#243)
Browse files Browse the repository at this point in the history
* fix: 방 상세 페이지 버그 수정

* refactor: 필터, AOP 수정
  • Loading branch information
ymkim97 authored Dec 3, 2023
1 parent a4a14ff commit 8917d5b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ private List<TodayCertificateRankResponse> uncompletedMembers(
List<TodayCertificateRankResponse> responses = new ArrayList<>();

List<Long> allMemberIds = participants.stream()
.filter(p -> p.getDeletedAt() == null)
.map(Participant::getMemberId)
.distinct()
.collect(Collectors.toList());
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/moabam/global/auth/filter/CorsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
FilterChain filterChain) throws ServletException, IOException {
String refer = httpServletRequest.getHeader("referer");
String origin = secureMatch(refer);
String refer = getReferer(httpServletRequest);

try {
if (Objects.isNull(origin)) {
if (Objects.isNull(refer)) {
throw new UnauthorizedException(ErrorMessage.INVALID_REQUEST_URL);
}
} catch (UnauthorizedException unauthorizedException) {
Expand All @@ -51,6 +50,8 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl
return;
}

String origin = secureMatch(refer);

httpServletResponse.setHeader("Access-Control-Allow-Origin", origin);
httpServletResponse.setHeader("Access-Control-Allow-Methods", ALLOWED_METHOD_NAMES);
httpServletResponse.setHeader("Access-Control-Allow-Headers", ALLOWED_HEADERS);
Expand All @@ -64,6 +65,10 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl
filterChain.doFilter(httpServletRequest, httpServletResponse);
}

public String getReferer(HttpServletRequest httpServletRequest) {
return httpServletRequest.getHeader("referer");
}

public String secureMatch(String refer) {
return allowOriginsConfig.origin().stream().filter(refer::contains).findFirst().orElse(null);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/moabam/global/common/util/LogAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.moabam.global.error.exception.UnauthorizedException;

import lombok.extern.slf4j.Slf4j;

@Aspect
Expand All @@ -17,7 +19,9 @@ public Object printExceptionLog(ProceedingJoinPoint joinPoint) throws Throwable
Object[] args = joinPoint.getArgs();
Exception exception = (Exception)args[0];

log.error("===== EXCEPTION LOG =====", exception);
if (!(exception instanceof UnauthorizedException)) {
log.error("===== EXCEPTION LOG =====", exception);
}

return joinPoint.proceed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.time.LocalDate;
import java.time.LocalDateTime;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile({"dev", "prod"})
public class SystemClockHolder implements ClockHolder {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void setUp() {
RestTemplate restTemplate = restTemplateBuilder.build();
ReflectionTestUtils.setField(oAuth2AuthorizationServerRequestService, "restTemplate", restTemplate);
mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
willReturn("http://localhost").given(corsFilter).secureMatch(any());
willReturn("http://localhost").given(corsFilter).getReferer(any());
}

@DisplayName("인가 코드 받기 위한 로그인 페이지 요청")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -19,11 +18,9 @@
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -51,7 +48,7 @@
import com.moabam.api.dto.room.CreateRoomRequest;
import com.moabam.api.dto.room.EnterRoomRequest;
import com.moabam.api.dto.room.ModifyRoomRequest;
import com.moabam.global.common.util.SystemClockHolder;
import com.moabam.global.common.util.ClockHolder;
import com.moabam.support.annotation.WithMember;
import com.moabam.support.common.WithoutFilterSupporter;
import com.moabam.support.fixture.BugFixture;
Expand Down Expand Up @@ -102,8 +99,8 @@ class RoomControllerTest extends WithoutFilterSupporter {
@Autowired
private InventoryRepository inventoryRepository;

@SpyBean
private SystemClockHolder clockHolder;
@Autowired
private ClockHolder clockHolder;

Member member;

Expand Down Expand Up @@ -431,7 +428,6 @@ void enter_room_with_password_success() throws Exception {
@Test
void enter_room_with_no_password_success() throws Exception {
// given
BDDMockito.given(clockHolder.times()).willReturn(LocalDateTime.of(2023, 12, 3, 14, 30, 0));
Room room = RoomFixture.room();

roomRepository.save(room);
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/moabam/support/common/TestClockHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.moabam.support.common;

import java.time.LocalDate;
import java.time.LocalDateTime;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.moabam.global.common.util.ClockHolder;

@Component
@Profile("test")
public class TestClockHolder implements ClockHolder {

@Override
public LocalDateTime times() {
return LocalDateTime.of(2023, 12, 3, 14, 30, 0);
}

@Override
public LocalDate date() {
return times().toLocalDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class WithoutFilterSupporter {
@BeforeEach
void setUpMock() {
willReturn("http://localhost:8080")
.given(corsFilter).secureMatch(any());
.given(corsFilter).getReferer(any());

willReturn(Optional.of(PathResolver.Path.builder()
.uri("/")
Expand Down

0 comments on commit 8917d5b

Please sign in to comment.