From b4a8f68eedb1113fbf67b9168be4d18be9a7a9a5 Mon Sep 17 00:00:00 2001 From: bflykky Date: Sun, 4 Aug 2024 14:42:51 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=82=B4=20=ED=9A=8C=EC=9B=90?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/controller/MemberController.java | 12 +++++++++++- .../naoman/domain/member/service/MemberService.java | 3 ++- .../domain/member/service/MemberServiceImpl.java | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/umc/naoman/domain/member/controller/MemberController.java b/src/main/java/com/umc/naoman/domain/member/controller/MemberController.java index 2f67fe0..c1023c7 100644 --- a/src/main/java/com/umc/naoman/domain/member/controller/MemberController.java +++ b/src/main/java/com/umc/naoman/domain/member/controller/MemberController.java @@ -2,11 +2,13 @@ import com.umc.naoman.domain.member.converter.MemberConverter; import com.umc.naoman.domain.member.dto.MemberResponse; +import com.umc.naoman.domain.member.dto.MemberResponse.MemberInfo; import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.member.service.MemberService; import com.umc.naoman.global.error.ErrorResponse; import com.umc.naoman.global.result.ResultResponse; import com.umc.naoman.global.result.code.MemberResultCode; +import com.umc.naoman.global.security.annotation.LoginMember; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -18,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static com.umc.naoman.global.result.code.MemberResultCode.CHECK_MEMBER_REGISTRATION; + @RestController @RequestMapping("/members") @Tag(name = "회원 API", description = "회원 도메인의 API입니다.") @@ -36,12 +40,18 @@ public class MemberController { (responseCode = "EM001", description = "해당 memberId를 가진 회원이 존재하지 않습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))), }) - public ResultResponse getMemberInfo(@PathVariable(name = "memberId") Long memberId) { + public ResultResponse getMemberInfo(@PathVariable(name = "memberId") Long memberId) { Member member = memberService.findMember(memberId); return ResultResponse.of(MemberResultCode.MEMBER_INFO, memberConverter.toMemberInfo(member)); } + @GetMapping("/my") + @Operation(summary = "내 회원정보 조회 API", description = "자신의 회원 정보를 조회하는 API입니다.") + public ResultResponse checkSignup(@LoginMember Member member) { + return ResultResponse.of(CHECK_MEMBER_REGISTRATION, memberService.getMyInfo(member)); + } + @GetMapping("/terms/{memberId}") @Operation(summary = "마케팅 약관 동의 여부 조회 API", description = "[PathVariable]\n memberId\n[request]\n" + "[response]\n 마케팅 동의 여부 -> 동의 => true, 비동의 => false") diff --git a/src/main/java/com/umc/naoman/domain/member/service/MemberService.java b/src/main/java/com/umc/naoman/domain/member/service/MemberService.java index 53c4a6c..f9c26d1 100644 --- a/src/main/java/com/umc/naoman/domain/member/service/MemberService.java +++ b/src/main/java/com/umc/naoman/domain/member/service/MemberService.java @@ -5,6 +5,7 @@ import com.umc.naoman.domain.member.dto.MemberRequest.SignupRequest; import com.umc.naoman.domain.member.dto.MemberResponse.CheckMemberRegistration; import com.umc.naoman.domain.member.dto.MemberResponse.LoginInfo; +import com.umc.naoman.domain.member.dto.MemberResponse.MemberInfo; import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.member.entity.SocialType; @@ -16,5 +17,5 @@ public interface MemberService { LoginInfo signup(SignupRequest request); LoginInfo signup(String tempMemberInfo, MarketingAgreedRequest request); LoginInfo login(LoginRequest request); - // MyPageInfo getMyPageInfo(Member member); + MemberInfo getMyInfo(Member member); } diff --git a/src/main/java/com/umc/naoman/domain/member/service/MemberServiceImpl.java b/src/main/java/com/umc/naoman/domain/member/service/MemberServiceImpl.java index b76e774..c00337e 100644 --- a/src/main/java/com/umc/naoman/domain/member/service/MemberServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/member/service/MemberServiceImpl.java @@ -6,6 +6,7 @@ import com.umc.naoman.domain.member.dto.MemberRequest.SignupRequest; import com.umc.naoman.domain.member.dto.MemberResponse.CheckMemberRegistration; import com.umc.naoman.domain.member.dto.MemberResponse.LoginInfo; +import com.umc.naoman.domain.member.dto.MemberResponse.MemberInfo; import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.member.entity.SocialType; import com.umc.naoman.domain.member.repository.MemberRepository; @@ -100,4 +101,9 @@ public LoginInfo login(LoginRequest request) { return memberConverter.toLoginInfo(memberId, accessToken, refreshToken); } + + @Override + public MemberInfo getMyInfo(Member member) { + return memberConverter.toMemberInfo(member); + } } From a8e4290d8b2086467c6b27334c14cbef26899825 Mon Sep 17 00:00:00 2001 From: bflykky Date: Sun, 4 Aug 2024 15:03:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=9D=B8=EC=A6=9D=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EC=9D=80=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EB=A5=BC=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20Aut?= =?UTF-8?q?henticationEntryPoint=20=EA=B5=AC=ED=98=84=EC=B2=B4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/error/code/GlobalErrorCode.java | 1 + .../global/security/SecurityConfig.java | 7 +++- .../CustomAuthenticationEntryPoint.java | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/umc/naoman/global/security/handler/CustomAuthenticationEntryPoint.java diff --git a/src/main/java/com/umc/naoman/global/error/code/GlobalErrorCode.java b/src/main/java/com/umc/naoman/global/error/code/GlobalErrorCode.java index 35aff4e..5f15172 100644 --- a/src/main/java/com/umc/naoman/global/error/code/GlobalErrorCode.java +++ b/src/main/java/com/umc/naoman/global/error/code/GlobalErrorCode.java @@ -22,6 +22,7 @@ public enum GlobalErrorCode implements ErrorCode { INTERNAL_SERVER_ERROR(500, "EG051", "내부 서버 오류입니다."), UNDEFINED_ERROR(400, "EG100", "정의되지 않은 에러입니다."), CLIENT_REGISTRATION_NOT_FOUND(400, "EM000", "해당 registrationId를 가진 ClientRegistration이 존재하지 않습니다."), + UNAUTHORIZED(401, "EG000", "인증되지 않은 사용자의 요청입니다. 로그인해 주세요."); ; private final int status; diff --git a/src/main/java/com/umc/naoman/global/security/SecurityConfig.java b/src/main/java/com/umc/naoman/global/security/SecurityConfig.java index cb53b09..734298f 100644 --- a/src/main/java/com/umc/naoman/global/security/SecurityConfig.java +++ b/src/main/java/com/umc/naoman/global/security/SecurityConfig.java @@ -4,6 +4,7 @@ import com.umc.naoman.domain.member.service.redis.RefreshTokenService; import com.umc.naoman.global.security.filter.JwtAuthenticationFilter; import com.umc.naoman.global.security.handler.CustomAccessDeniedHandler; +import com.umc.naoman.global.security.handler.CustomAuthenticationEntryPoint; import com.umc.naoman.global.security.handler.OAuth2LoginSuccessHandler; import com.umc.naoman.global.security.repository.OAuth2AuthorizationRequestBasedOnCookieRepository; import com.umc.naoman.global.security.service.CustomOAuth2UserService; @@ -26,6 +27,7 @@ public class SecurityConfig { private final CustomOAuth2UserService customOAuth2UserService; private final RefreshTokenService refreshTokenService; private final CustomAccessDeniedHandler customAccessDeniedHandler; + private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint; private final JwtUtils jwtUtils; @Bean @@ -59,14 +61,15 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { "/v3/api-docs/**").permitAll() .anyRequest().authenticated() ) - .exceptionHandling(exception -> exception.accessDeniedHandler(customAccessDeniedHandler)) + .exceptionHandling(exception -> exception + .accessDeniedHandler(customAccessDeniedHandler) + .authenticationEntryPoint(customAuthenticationEntryPoint)) .oauth2Login(oauth2 -> oauth2 .authorizationEndpoint(endpoint -> endpoint .authorizationRequestRepository(oAuth2AuthorizationRequestBasedOnCookieRepository())) .userInfoEndpoint(userInfoEndpointConfig -> userInfoEndpointConfig.userService(customOAuth2UserService)) .successHandler(oAuth2LoginSuccessHandler()) - .loginPage("/auth/login") ) .addFilterAfter(new JwtAuthenticationFilter(jwtUtils), OAuth2LoginAuthenticationFilter.class); diff --git a/src/main/java/com/umc/naoman/global/security/handler/CustomAuthenticationEntryPoint.java b/src/main/java/com/umc/naoman/global/security/handler/CustomAuthenticationEntryPoint.java new file mode 100644 index 0000000..0fa4b07 --- /dev/null +++ b/src/main/java/com/umc/naoman/global/security/handler/CustomAuthenticationEntryPoint.java @@ -0,0 +1,37 @@ +package com.umc.naoman.global.security.handler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.umc.naoman.global.error.ErrorResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.http.MediaType; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.nio.charset.Charset; + +import static com.umc.naoman.global.error.code.GlobalErrorCode.UNAUTHORIZED; + +@Component +public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { + private final ObjectMapper objectMapper = new ObjectMapper(); + @Override + public void commence(HttpServletRequest request, HttpServletResponse response, + AuthenticationException authException) throws IOException, ServletException { + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + response.setStatus(UNAUTHORIZED.getStatus()); + response.setCharacterEncoding(Charset.defaultCharset().name()); + + ErrorResponse errorResponse = ErrorResponse.builder() + .status(response.getStatus()) + .code(UNAUTHORIZED.getMessage()) + .message(authException.getMessage()) + .data(null) + .build(); + + response.getWriter().write(objectMapper.writeValueAsString(errorResponse)); + } +} \ No newline at end of file