From b4672fdf7e29a2fe155571e8294070f439bdd927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Tue, 16 Jan 2024 01:52:16 +0900 Subject: [PATCH] =?UTF-8?q?[HOTFIX]=20stomp=20=ED=97=A4=EB=8D=94=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/docs/member.adoc | 3 + .../config/security/SecurityConfig.java | 1 + .../backend/config/socket/StompConfig.java | 9 --- .../backend/config/socket/StompHandler.java | 55 ------------------- .../location/dto/request/LocationRequest.java | 3 + .../location/service/LocationService.java | 8 +-- .../member/controller/MemberController.java | 7 ++- .../member/dto/response/IdResponse.java | 12 ++++ .../domain/member/service/MemberService.java | 5 ++ .../controller/MemberControllerTest.java | 50 +++++++++++++---- 10 files changed, 71 insertions(+), 82 deletions(-) delete mode 100644 backend/src/main/java/com/twtw/backend/config/socket/StompHandler.java create mode 100644 backend/src/main/java/com/twtw/backend/domain/member/dto/response/IdResponse.java diff --git a/backend/src/docs/member.adoc b/backend/src/docs/member.adoc index bd816fb5..69cf1e3a 100644 --- a/backend/src/docs/member.adoc +++ b/backend/src/docs/member.adoc @@ -10,3 +10,6 @@ operation::get duplicate nickname[snippets='http-request,http-response'] === 닉네임으로 멤버 검색 operation::get member nickname[snippets='http-request,http-response'] + +=== 닉네임으로 멤버 검색 +operation::get member id[snippets='http-request,http-response'] diff --git a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java index 77bcc1c0..ba4e2107 100644 --- a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java @@ -35,6 +35,7 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception { "auth/save", "auth/login", "member/duplicate/**", + "plan/**", "actuator/**") .permitAll() .anyRequest() diff --git a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java index b479852b..bbe5ac30 100644 --- a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java @@ -1,12 +1,9 @@ package com.twtw.backend.config.socket; import com.twtw.backend.global.properties.RabbitMQProperties; - import lombok.RequiredArgsConstructor; - import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -import org.springframework.messaging.simp.config.ChannelRegistration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.util.AntPathMatcher; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; @@ -20,7 +17,6 @@ public class StompConfig implements WebSocketMessageBrokerConfigurer { private final RabbitMQProperties rabbitMQProperties; - private final StompHandler stompHandler; @Override public void registerStompEndpoints(final StompEndpointRegistry registry) { @@ -39,9 +35,4 @@ public void configureMessageBroker(final MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/pub"); } - - @Override - public void configureClientInboundChannel(ChannelRegistration registration) { - registration.interceptors(stompHandler); - } } diff --git a/backend/src/main/java/com/twtw/backend/config/socket/StompHandler.java b/backend/src/main/java/com/twtw/backend/config/socket/StompHandler.java deleted file mode 100644 index aa46cdd6..00000000 --- a/backend/src/main/java/com/twtw/backend/config/socket/StompHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.twtw.backend.config.socket; - -import com.twtw.backend.config.security.jwt.TokenProvider; - -import lombok.RequiredArgsConstructor; - -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; -import org.springframework.messaging.Message; -import org.springframework.messaging.MessageChannel; -import org.springframework.messaging.simp.stomp.StompCommand; -import org.springframework.messaging.simp.stomp.StompHeaderAccessor; -import org.springframework.messaging.support.ChannelInterceptor; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Component; - -import java.util.Optional; - -@Component -@RequiredArgsConstructor -@Order(Ordered.HIGHEST_PRECEDENCE + 99) -public class StompHandler implements ChannelInterceptor { - - private static final String AUTHORIZATION_HEADER = "Authorization"; - private static final String BEARER_PREFIX = "Bearer "; - private final TokenProvider tokenProvider; - - @Override - public Message preSend(final Message message, final MessageChannel channel) { - final StompHeaderAccessor acessor = StompHeaderAccessor.wrap(message); - - if (StompCommand.CONNECT == acessor.getCommand()) { - final Optional headerValue = - Optional.ofNullable(acessor.getFirstNativeHeader(AUTHORIZATION_HEADER)); - resolveToken(headerValue) - .ifPresent( - header -> { - tokenProvider.validateToken(header); - SecurityContextHolder.getContext() - .setAuthentication(tokenProvider.getAuthentication(header)); - }); - } - return message; - } - - private Optional resolveToken(final Optional headerValue) { - return headerValue.map( - header -> { - if (header.startsWith(BEARER_PREFIX)) { - return header.substring(7); - } - return header; - }); - } -} diff --git a/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java b/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java index edb19196..c7064225 100644 --- a/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java +++ b/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java @@ -4,10 +4,13 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.UUID; + @Getter @NoArgsConstructor @AllArgsConstructor public class LocationRequest { + private UUID memberId; private String nickname; private Double longitude; private Double latitude; diff --git a/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java index 163d0fdd..f4620984 100644 --- a/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java +++ b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java @@ -5,12 +5,10 @@ import com.twtw.backend.domain.location.dto.response.LocationResponse; import com.twtw.backend.domain.location.mapper.LocationMapper; import com.twtw.backend.domain.member.entity.Member; -import com.twtw.backend.domain.member.service.AuthService; +import com.twtw.backend.domain.member.service.MemberService; import com.twtw.backend.domain.plan.entity.Plan; import com.twtw.backend.domain.plan.service.PlanService; - import lombok.RequiredArgsConstructor; - import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,13 +20,13 @@ public class LocationService { private final LocationMapper locationMapper; - private final AuthService authService; + private final MemberService memberService; private final PlanService planService; private final GeoService geoService; @Transactional public LocationResponse addInfo(final UUID planId, final LocationRequest locationRequest) { - final Member member = authService.getMemberByJwt(); + final Member member = memberService.getMemberById(locationRequest.getMemberId()); final Plan plan = planService.getPlanEntity(planId); plan.updateMemberLocation( diff --git a/backend/src/main/java/com/twtw/backend/domain/member/controller/MemberController.java b/backend/src/main/java/com/twtw/backend/domain/member/controller/MemberController.java index 64710b22..18a80deb 100644 --- a/backend/src/main/java/com/twtw/backend/domain/member/controller/MemberController.java +++ b/backend/src/main/java/com/twtw/backend/domain/member/controller/MemberController.java @@ -1,9 +1,9 @@ package com.twtw.backend.domain.member.controller; import com.twtw.backend.domain.member.dto.response.DuplicateNicknameResponse; +import com.twtw.backend.domain.member.dto.response.IdResponse; import com.twtw.backend.domain.member.dto.response.MemberResponse; import com.twtw.backend.domain.member.service.MemberService; - import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -28,4 +28,9 @@ public ResponseEntity> searchMemberByNickname( @RequestParam(name = "nickname") String nickname) { return ResponseEntity.ok(memberService.getMemberByNickname(nickname)); } + + @GetMapping("/me") + public ResponseEntity getMemberByJwt() { + return ResponseEntity.ok(memberService.getMemberId()); + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/member/dto/response/IdResponse.java b/backend/src/main/java/com/twtw/backend/domain/member/dto/response/IdResponse.java new file mode 100644 index 00000000..b806cec4 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/member/dto/response/IdResponse.java @@ -0,0 +1,12 @@ +package com.twtw.backend.domain.member.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.UUID; + +@Getter +@AllArgsConstructor +public class IdResponse { + private UUID id; +} diff --git a/backend/src/main/java/com/twtw/backend/domain/member/service/MemberService.java b/backend/src/main/java/com/twtw/backend/domain/member/service/MemberService.java index b614c318..2399c5eb 100644 --- a/backend/src/main/java/com/twtw/backend/domain/member/service/MemberService.java +++ b/backend/src/main/java/com/twtw/backend/domain/member/service/MemberService.java @@ -1,6 +1,7 @@ package com.twtw.backend.domain.member.service; import com.twtw.backend.domain.member.dto.response.DuplicateNicknameResponse; +import com.twtw.backend.domain.member.dto.response.IdResponse; import com.twtw.backend.domain.member.dto.response.MemberResponse; import com.twtw.backend.domain.member.entity.Member; import com.twtw.backend.domain.member.mapper.MemberMapper; @@ -60,4 +61,8 @@ public List getMemberResponses(final Plan plan) { public List getMembersByIds(final List friendMemberIds) { return memberRepository.findAllById(friendMemberIds); } + + public IdResponse getMemberId() { + return new IdResponse(authService.getMemberByJwt().getId()); + } } diff --git a/backend/src/test/java/com/twtw/backend/domain/member/controller/MemberControllerTest.java b/backend/src/test/java/com/twtw/backend/domain/member/controller/MemberControllerTest.java index e152b3c8..54ef3b40 100644 --- a/backend/src/test/java/com/twtw/backend/domain/member/controller/MemberControllerTest.java +++ b/backend/src/test/java/com/twtw/backend/domain/member/controller/MemberControllerTest.java @@ -1,21 +1,10 @@ package com.twtw.backend.domain.member.controller; -import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest; -import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; -import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import com.twtw.backend.domain.member.dto.response.DuplicateNicknameResponse; +import com.twtw.backend.domain.member.dto.response.IdResponse; import com.twtw.backend.domain.member.dto.response.MemberResponse; import com.twtw.backend.domain.member.service.MemberService; import com.twtw.backend.support.docs.RestDocsTest; - import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -26,6 +15,16 @@ import java.util.List; import java.util.UUID; +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest; +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @DisplayName("MemberController의") @WebMvcTest(MemberController.class) public class MemberControllerTest extends RestDocsTest { @@ -84,4 +83,31 @@ void searchMemberByNickname() throws Exception { getDocumentRequest(), getDocumentResponse())); } + + @Test + @DisplayName("Member id가 조회되는가") + void getMemberId() throws Exception { + // given + final IdResponse expected = new IdResponse(UUID.randomUUID()); + given(memberService.getMemberId()).willReturn(expected); + + // when + final ResultActions perform = + mockMvc.perform( + get("/member/me") + .contentType(MediaType.APPLICATION_JSON) + .header( + "Authorization", + "Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh")); + + // then + perform.andExpect(status().isOk()); + + perform.andDo(print()) + .andDo( + document( + "get member id", + getDocumentRequest(), + getDocumentResponse())); + } }