From 45917f9105d32f33ec9b3423087379b8c58c834d Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Mon, 1 Apr 2024 21:11:25 +0900 Subject: [PATCH 1/5] =?UTF-8?q?Fix:=20=ED=86=A0=ED=81=B0=20=EB=A7=8C?= =?UTF-8?q?=EB=A3=8C=EC=8B=9C=20http=20=EC=83=81=ED=83=9C=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/numberone/backend/filter/JwtExceptionFilter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/daepiro-auth/src/main/java/com/numberone/backend/filter/JwtExceptionFilter.java b/daepiro-auth/src/main/java/com/numberone/backend/filter/JwtExceptionFilter.java index 1ddb381..1b86896 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/filter/JwtExceptionFilter.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/filter/JwtExceptionFilter.java @@ -26,10 +26,10 @@ public class JwtExceptionFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { try { filterChain.doFilter(request, response); - } catch (ExpiredJwtException e){ - httpResponseProvider.setErrorResponse(response,HttpServletResponse.SC_FORBIDDEN, EXPIRED_TOKEN);//to do: 프론트와 협의 후 403에서 401로 수정 - }catch (JwtException e){ - httpResponseProvider.setErrorResponse(response,HttpServletResponse.SC_UNAUTHORIZED, INVALID_TOKEN); + } catch (ExpiredJwtException e) { + httpResponseProvider.setErrorResponse(response, HttpServletResponse.SC_UNAUTHORIZED, EXPIRED_TOKEN); + } catch (JwtException e) { + httpResponseProvider.setErrorResponse(response, HttpServletResponse.SC_UNAUTHORIZED, INVALID_TOKEN); } } } From 099abd1c1c914ccca796bc7ba40f0da50c13c971 Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Mon, 1 Apr 2024 21:12:11 +0900 Subject: [PATCH 2/5] =?UTF-8?q?Rename:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Filter.java => KakaoAuthenticationFilter.java} | 15 +++++++-------- ...ider.java => KakaoAuthenticationProvider.java} | 13 +++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) rename daepiro-auth/src/main/java/com/numberone/backend/filter/{SocialAuthenticationFilter.java => KakaoAuthenticationFilter.java} (76%) rename daepiro-auth/src/main/java/com/numberone/backend/provider/{SocialAuthenticationProvider.java => KakaoAuthenticationProvider.java} (73%) diff --git a/daepiro-auth/src/main/java/com/numberone/backend/filter/SocialAuthenticationFilter.java b/daepiro-auth/src/main/java/com/numberone/backend/filter/KakaoAuthenticationFilter.java similarity index 76% rename from daepiro-auth/src/main/java/com/numberone/backend/filter/SocialAuthenticationFilter.java rename to daepiro-auth/src/main/java/com/numberone/backend/filter/KakaoAuthenticationFilter.java index 34bb155..183356e 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/filter/SocialAuthenticationFilter.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/filter/KakaoAuthenticationFilter.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.numberone.backend.handler.SocialAuthenticationFailureHandler; import com.numberone.backend.handler.SocialAuthenticationSuccessHandler; -import com.numberone.backend.provider.SocialAuthenticationProvider; +import com.numberone.backend.provider.KakaoAuthenticationProvider; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -17,24 +17,23 @@ import org.springframework.util.StreamUtils; import java.io.IOException; -import java.net.http.HttpHeaders; import java.nio.charset.StandardCharsets; import java.util.Map; @Component -public class SocialAuthenticationFilter extends AbstractAuthenticationProcessingFilter { +public class KakaoAuthenticationFilter extends AbstractAuthenticationProcessingFilter { private final ObjectMapper objectMapper; private static final String JSON_PARAM = "token"; private static final String REQUEST_URL = "/token/kakao"; private static final String REQUEST_METHOD = "POST"; - public SocialAuthenticationFilter(SocialAuthenticationProvider socialAuthenticationProvider, - SocialAuthenticationSuccessHandler socialAuthenticationSuccessHandler, - SocialAuthenticationFailureHandler socialAuthenticationFailureHandler, - ObjectMapper objectMapper) { + public KakaoAuthenticationFilter(KakaoAuthenticationProvider kakaoAuthenticationProvider, + SocialAuthenticationSuccessHandler socialAuthenticationSuccessHandler, + SocialAuthenticationFailureHandler socialAuthenticationFailureHandler, + ObjectMapper objectMapper) { super(new AntPathRequestMatcher(REQUEST_URL, REQUEST_METHOD)); - setAuthenticationManager(new ProviderManager(socialAuthenticationProvider)); + setAuthenticationManager(new ProviderManager(kakaoAuthenticationProvider)); setAuthenticationSuccessHandler(socialAuthenticationSuccessHandler); setAuthenticationFailureHandler(socialAuthenticationFailureHandler); this.objectMapper = objectMapper; diff --git a/daepiro-auth/src/main/java/com/numberone/backend/provider/SocialAuthenticationProvider.java b/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java similarity index 73% rename from daepiro-auth/src/main/java/com/numberone/backend/provider/SocialAuthenticationProvider.java rename to daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java index ba2194b..3b06ca7 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/provider/SocialAuthenticationProvider.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java @@ -1,7 +1,5 @@ package com.numberone.backend.provider; -import com.numberone.backend.config.AspectConfig; -import com.numberone.backend.exception.notfound.NotFoundMemberException; import com.numberone.backend.domain.member.entity.Member; import com.numberone.backend.domain.member.repository.MemberRepository; import com.numberone.backend.feign.KakaoFeign; @@ -11,28 +9,27 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; @Component @RequiredArgsConstructor -public class SocialAuthenticationProvider implements AuthenticationProvider { +public class KakaoAuthenticationProvider implements AuthenticationProvider { private final KakaoFeign kakaoFeign; private final MemberRepository memberRepository; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String token = (String) authentication.getPrincipal(); - Long socialId; + Long kakaoId; try { - socialId = kakaoFeign.getUserData(JwtProvider.PREFIX_BEARER + token).getId(); + kakaoId = kakaoFeign.getUserData(JwtProvider.PREFIX_BEARER + token).getId(); } catch (Exception e) { throw new BadCredentialsException("유효하지 않은 OAuth 토큰입니다."); } - Member member = memberRepository.findBySocialId(socialId) + Member member = memberRepository.findByKakaoId(kakaoId) .orElse(null); if (member == null) - member = memberRepository.save(Member.of(socialId)); + member = memberRepository.save(Member.ofKakao(kakaoId)); return UsernamePasswordAuthenticationToken.authenticated(member.getId(), null, null); } From 5cd716f9c7ec1510f6926b0ae76ddad6716ea36c Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Mon, 1 Apr 2024 21:12:43 +0900 Subject: [PATCH 3/5] =?UTF-8?q?Feat:=20=EB=84=A4=EC=9D=B4=EB=B2=84=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=95=84=ED=84=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 +- .../backend/config/SecurityConfig.java | 14 +++--- .../filter/NaverAuthenticationFilter.java | 49 +++++++++++++++++++ .../provider/NaverAuthenticationProvider.java | 41 ++++++++++++++++ .../numberone/backend/feign/NaverFeign.java | 11 +++++ .../numberone/backend/feign/NaverIdDto.java | 18 +++++++ .../src/main/resources/application-common.yml | 7 +++ .../backend/domain/member/entity/Member.java | 33 ++++++++----- .../member/repository/MemberRepository.java | 10 ++-- 9 files changed, 158 insertions(+), 27 deletions(-) create mode 100644 daepiro-auth/src/main/java/com/numberone/backend/filter/NaverAuthenticationFilter.java create mode 100644 daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java create mode 100644 daepiro-common/src/main/java/com/numberone/backend/feign/NaverFeign.java create mode 100644 daepiro-common/src/main/java/com/numberone/backend/feign/NaverIdDto.java diff --git a/daepiro-app/src/main/resources/application.yml b/daepiro-app/src/main/resources/application.yml index 7d7cd91..1d9ced2 100644 --- a/daepiro-app/src/main/resources/application.yml +++ b/daepiro-app/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: dev + active: local group: local: core,redis,auth,crawler,common,api dev: core,redis,auth,crawler,common,api diff --git a/daepiro-auth/src/main/java/com/numberone/backend/config/SecurityConfig.java b/daepiro-auth/src/main/java/com/numberone/backend/config/SecurityConfig.java index 87f810e..b3cc25f 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/config/SecurityConfig.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/config/SecurityConfig.java @@ -1,9 +1,6 @@ package com.numberone.backend.config; -import com.numberone.backend.filter.JwtAccessFilter; -import com.numberone.backend.filter.JwtExceptionFilter; -import com.numberone.backend.filter.JwtRefreshFilter; -import com.numberone.backend.filter.SocialAuthenticationFilter; +import com.numberone.backend.filter.*; import com.numberone.backend.handler.CustomAccessDeniedHandler; import com.numberone.backend.handler.CustomAuthenticationEntryPoint; import lombok.RequiredArgsConstructor; @@ -13,12 +10,12 @@ import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.access.ExceptionTranslationFilter; -import org.springframework.security.web.authentication.logout.LogoutFilter; @Configuration @RequiredArgsConstructor public class SecurityConfig { - private final SocialAuthenticationFilter socialAuthenticationFilter; + private final KakaoAuthenticationFilter kakaoAuthenticationFilter; + private final NaverAuthenticationFilter naverAuthenticationFilter; private final JwtAccessFilter jwtAccessFilter; private final JwtRefreshFilter jwtRefreshFilter; private final JwtExceptionFilter jwtExceptionFilter; @@ -40,8 +37,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { "/notification/send-fcm", "/hello").permitAll() .anyRequest().authenticated()) - .addFilterBefore(socialAuthenticationFilter, ExceptionTranslationFilter.class) - .addFilterBefore(jwtAccessFilter, SocialAuthenticationFilter.class) + .addFilterBefore(naverAuthenticationFilter, ExceptionTranslationFilter.class) + .addFilterBefore(kakaoAuthenticationFilter, NaverAuthenticationFilter.class) + .addFilterBefore(jwtAccessFilter, KakaoAuthenticationFilter.class) .addFilterBefore(jwtRefreshFilter, JwtAccessFilter.class) .addFilterBefore(jwtExceptionFilter, JwtRefreshFilter.class) .exceptionHandling(c -> c diff --git a/daepiro-auth/src/main/java/com/numberone/backend/filter/NaverAuthenticationFilter.java b/daepiro-auth/src/main/java/com/numberone/backend/filter/NaverAuthenticationFilter.java new file mode 100644 index 0000000..9e2c77c --- /dev/null +++ b/daepiro-auth/src/main/java/com/numberone/backend/filter/NaverAuthenticationFilter.java @@ -0,0 +1,49 @@ +package com.numberone.backend.filter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.numberone.backend.handler.SocialAuthenticationFailureHandler; +import com.numberone.backend.handler.SocialAuthenticationSuccessHandler; +import com.numberone.backend.provider.NaverAuthenticationProvider; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.authentication.ProviderManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import org.springframework.stereotype.Component; +import org.springframework.util.StreamUtils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +@Component +public class NaverAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private final ObjectMapper objectMapper; + private static final String JSON_PARAM = "token"; + private static final String REQUEST_URL = "/token/naver"; + private static final String REQUEST_METHOD = "POST"; + + public NaverAuthenticationFilter(NaverAuthenticationProvider naverAuthenticationProvider, + SocialAuthenticationSuccessHandler socialAuthenticationSuccessHandler, + SocialAuthenticationFailureHandler socialAuthenticationFailureHandler, + ObjectMapper objectMapper){ + super(new AntPathRequestMatcher(REQUEST_URL, REQUEST_METHOD)); + setAuthenticationManager(new ProviderManager(naverAuthenticationProvider)); + setAuthenticationSuccessHandler(socialAuthenticationSuccessHandler); + setAuthenticationFailureHandler(socialAuthenticationFailureHandler); + this.objectMapper = objectMapper; + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { + String requestBody = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8); + Map requestBodyMap = objectMapper.readValue(requestBody, Map.class); + String token = requestBodyMap.get(JSON_PARAM); + Authentication authentication = UsernamePasswordAuthenticationToken.unauthenticated(token, null); + return getAuthenticationManager().authenticate(authentication); + } +} diff --git a/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java b/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java new file mode 100644 index 0000000..ac59fad --- /dev/null +++ b/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java @@ -0,0 +1,41 @@ +package com.numberone.backend.provider; + +import com.numberone.backend.domain.member.entity.Member; +import com.numberone.backend.domain.member.repository.MemberRepository; +import com.numberone.backend.feign.NaverFeign; +import lombok.RequiredArgsConstructor; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class NaverAuthenticationProvider implements AuthenticationProvider { + private final NaverFeign naverFeign; + private final MemberRepository memberRepository; + + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + String token = (String) authentication.getPrincipal(); + String naverId; + try { + naverId = naverFeign.getUserData(JwtProvider.PREFIX_BEARER + token).getResponse().getId(); + } catch (Exception e) { + throw new BadCredentialsException("유효하지 않은 OAuth 토큰입니다."); + } + Member member = memberRepository.findByNaverId(naverId) + .orElse(null); + if (member == null) + member = memberRepository.save(Member.ofNaver(naverId)); + return UsernamePasswordAuthenticationToken.authenticated(member.getId(), null, null); + } + + @Override + public boolean supports(Class authentication) { + return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); + } +} diff --git a/daepiro-common/src/main/java/com/numberone/backend/feign/NaverFeign.java b/daepiro-common/src/main/java/com/numberone/backend/feign/NaverFeign.java new file mode 100644 index 0000000..88bfd41 --- /dev/null +++ b/daepiro-common/src/main/java/com/numberone/backend/feign/NaverFeign.java @@ -0,0 +1,11 @@ +package com.numberone.backend.feign; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; + +@FeignClient(name = "naverFeign", url = "${spring.naver.api-url}") +public interface NaverFeign { + @GetMapping(value = "${spring.naver.token-info-url}") + NaverIdDto getUserData(@RequestHeader(name = "Authorization") String token); +} diff --git a/daepiro-common/src/main/java/com/numberone/backend/feign/NaverIdDto.java b/daepiro-common/src/main/java/com/numberone/backend/feign/NaverIdDto.java new file mode 100644 index 0000000..128ba62 --- /dev/null +++ b/daepiro-common/src/main/java/com/numberone/backend/feign/NaverIdDto.java @@ -0,0 +1,18 @@ +package com.numberone.backend.feign; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class NaverIdDto { + private Response response; + + @Getter + @NoArgsConstructor(access = AccessLevel.PROTECTED) + public static class Response { + private String id; + } +} diff --git a/daepiro-common/src/main/resources/application-common.yml b/daepiro-common/src/main/resources/application-common.yml index f402ce2..2ef6202 100644 --- a/daepiro-common/src/main/resources/application-common.yml +++ b/daepiro-common/src/main/resources/application-common.yml @@ -9,6 +9,9 @@ spring: map: client-id: ${KAKAO_MAP_CLIENT_ID} map-api-url: ${KAKAO_MAP_API_URL} + naver: + api-url: ${NAVER_API_URL} + token-info-url: ${NAVER_TOKEN_INFO_URL} cloud: aws: @@ -44,6 +47,10 @@ spring: client-id: ${KAKAO_MAP_CLIENT_ID} map-api-url: ${KAKAO_MAP_API_URL} + naver: + api-url: ${NAVER_API_URL} + token-info-url: ${NAVER_TOKEN_INFO_URL} + cloud: aws: credentials: diff --git a/daepiro-core/src/main/java/com/numberone/backend/domain/member/entity/Member.java b/daepiro-core/src/main/java/com/numberone/backend/domain/member/entity/Member.java index aee77d5..84d1b6a 100644 --- a/daepiro-core/src/main/java/com/numberone/backend/domain/member/entity/Member.java +++ b/daepiro-core/src/main/java/com/numberone/backend/domain/member/entity/Member.java @@ -31,11 +31,15 @@ public class Member extends BaseTimeEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Comment("로그인 유형") @Enumerated(EnumType.STRING) private LoginType loginType; - @Comment("소셜 id") - private Long socialId; + @Comment("카카오 id") + private Long kakaoId; + + @Comment("네이버 id") + private String naverId; @Comment("닉네임") private String nickName; @@ -111,22 +115,29 @@ public void updateSession(Boolean session) { } @Builder - public Member(Long socialId, String nickName, String realName, Integer heartCnt, String fcmToken, Boolean session) { - this.socialId = socialId; - this.nickName = nickName; - this.realName = realName; - this.heartCnt = heartCnt; - this.fcmToken = fcmToken; + public Member(Long kakaoId, String naverId, String email, LoginType loginType) { + this.kakaoId = kakaoId; + this.naverId = naverId; + this.email=email; + this.loginType = loginType; + this.heartCnt = 0; this.session = true; this.latitude = 0D; this.longitude = 0D; isOnboarding = false; } - public static Member of(Long socialId) { + public static Member ofKakao(Long kakaoId) { + return Member.builder() + .kakaoId(kakaoId) + .loginType(LoginType.KAKAO) + .build(); + } + + public static Member ofNaver(String naverId){ return Member.builder() - .socialId(socialId) - .heartCnt(0) + .naverId(naverId) + .loginType(LoginType.NAVER) .build(); } diff --git a/daepiro-core/src/main/java/com/numberone/backend/domain/member/repository/MemberRepository.java b/daepiro-core/src/main/java/com/numberone/backend/domain/member/repository/MemberRepository.java index 039664a..b12af93 100644 --- a/daepiro-core/src/main/java/com/numberone/backend/domain/member/repository/MemberRepository.java +++ b/daepiro-core/src/main/java/com/numberone/backend/domain/member/repository/MemberRepository.java @@ -8,13 +8,9 @@ import java.util.Optional; public interface MemberRepository extends JpaRepository, MemberRepositoryCustom { - Optional findBySocialId(Long socialId); + Optional findByKakaoId(Long kakaoId); - boolean existsById(Long id); - - boolean existsBySocialId(Long socialId); + Optional findByNaverId(String naverId); - List findByLv1(String Lv1); - - List findByLv2(String Lv2); + boolean existsById(Long id); } From c91db6d1de992fb293db9f2885fdadb17b28d956 Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Mon, 1 Apr 2024 21:23:33 +0900 Subject: [PATCH 4/5] =?UTF-8?q?Feat:=20Swagger=EC=97=90=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B2=84=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 +- .../swagger/SwaggerDocumentController.java | 36 +++++++++---------- ...eshRequest.java => LoginRequestDummy.java} | 2 +- ...nRequest.java => RefreshRequestDummy.java} | 2 +- ...nResponse.java => LoginResponseDummy.java} | 2 +- ...esponse.java => RefreshResponseDummy.java} | 2 +- 6 files changed, 23 insertions(+), 23 deletions(-) rename daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/{JwtRefreshRequest.java => LoginRequestDummy.java} (87%) rename daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/{LoginRequest.java => RefreshRequestDummy.java} (86%) rename daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/{LoginResponse.java => LoginResponseDummy.java} (84%) rename daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/{JwtRefreshResponse.java => RefreshResponseDummy.java} (80%) diff --git a/daepiro-app/src/main/resources/application.yml b/daepiro-app/src/main/resources/application.yml index 1d9ced2..7d7cd91 100644 --- a/daepiro-app/src/main/resources/application.yml +++ b/daepiro-app/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: local + active: dev group: local: core,redis,auth,crawler,common,api dev: core,redis,auth,crawler,common,api diff --git a/daepiro-auth/src/main/java/com/numberone/backend/swagger/SwaggerDocumentController.java b/daepiro-auth/src/main/java/com/numberone/backend/swagger/SwaggerDocumentController.java index 785912e..873b8cd 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/swagger/SwaggerDocumentController.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/swagger/SwaggerDocumentController.java @@ -1,10 +1,10 @@ package com.numberone.backend.swagger; -import com.numberone.backend.swagger.dto.request.JwtRefreshRequest; -import com.numberone.backend.swagger.dto.request.LoginRequest; -import com.numberone.backend.swagger.dto.response.JwtRefreshResponse; -import com.numberone.backend.swagger.dto.response.LoginResponse; +import com.numberone.backend.swagger.dto.request.RefreshRequestDummy; +import com.numberone.backend.swagger.dto.request.LoginRequestDummy; +import com.numberone.backend.swagger.dto.response.RefreshResponseDummy; +import com.numberone.backend.swagger.dto.response.LoginResponseDummy; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.PostMapping; @@ -26,22 +26,22 @@ public class SwaggerDocumentController {// 필터에서 처리하는 api의 경 이후 서버에 API 요청시 이 JWT 토큰을 같이 담아서 요청해야 정상적으로 API가 호출 됩니다. """) @PostMapping("/kakao") - public LoginResponse loginKakao(@RequestBody LoginRequest loginRequest) { + public LoginResponseDummy loginKakao(@RequestBody LoginRequestDummy loginRequestDummy) { return null; } -// @Operation(summary = "네이버 토큰을 이용하여 서버 JWT 토큰 발급받기", description = -// """ -// 네이버 토큰을 body 에 담아서 post 요청 해주세요. -// -// 앞으로 서버 요청 시에 사용할 수 있는 JWT 토큰이 발급됩니다. -// -// 이후 서버에 API 요청시 이 JWT 토큰을 같이 담아서 요청해야 정상적으로 API가 호출 됩니다. -// """) -// @PostMapping("/naver") -// public GetTokenResponse loginNaver(@RequestBody @Valid GetTokenRequest tokenRequest) { -// return tokenService.loginNaver(tokenRequest); -// } + @Operation(summary = "네이버 토큰을 이용하여 서버 JWT 토큰 발급받기", description = + """ + 네이버 토큰을 body 에 담아서 post 요청 해주세요. + + 앞으로 서버 요청 시에 사용할 수 있는 JWT 토큰이 발급됩니다. + + 이후 서버에 API 요청시 이 JWT 토큰을 같이 담아서 요청해야 정상적으로 API가 호출 됩니다. + """) + @PostMapping("/naver") + public LoginResponseDummy loginNaver(@RequestBody LoginRequestDummy loginRequestDummy) { + return null; + } @Operation(summary = "만료된 JWT 토큰 갱신하기", description = """ @@ -50,7 +50,7 @@ public LoginResponse loginKakao(@RequestBody LoginRequest loginRequest) { 새로 사용할 수 있는 JWT 토큰이 발급됩니다. """) @PostMapping("/refresh") - public JwtRefreshResponse refresh(@RequestBody JwtRefreshRequest jwtRefreshRequest) { + public RefreshResponseDummy refresh(@RequestBody RefreshRequestDummy refreshRequestDummy) { return null; } } diff --git a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/JwtRefreshRequest.java b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequestDummy.java similarity index 87% rename from daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/JwtRefreshRequest.java rename to daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequestDummy.java index 2fe035b..dc90e48 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/JwtRefreshRequest.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequestDummy.java @@ -6,6 +6,6 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class JwtRefreshRequest { +public class LoginRequestDummy { private String token; } diff --git a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequest.java b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/RefreshRequestDummy.java similarity index 86% rename from daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequest.java rename to daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/RefreshRequestDummy.java index b759d28..c3cc011 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/LoginRequest.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/request/RefreshRequestDummy.java @@ -6,6 +6,6 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class LoginRequest { +public class RefreshRequestDummy { private String token; } diff --git a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponse.java b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponseDummy.java similarity index 84% rename from daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponse.java rename to daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponseDummy.java index 9ecae5e..145782d 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponse.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/LoginResponseDummy.java @@ -3,7 +3,7 @@ import lombok.Getter; @Getter -public class LoginResponse { +public class LoginResponseDummy { private String accessToken; private String refreshToken; private Boolean isOnboarding; diff --git a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/JwtRefreshResponse.java b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/RefreshResponseDummy.java similarity index 80% rename from daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/JwtRefreshResponse.java rename to daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/RefreshResponseDummy.java index 4cb69d9..5749049 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/JwtRefreshResponse.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/swagger/dto/response/RefreshResponseDummy.java @@ -3,7 +3,7 @@ import lombok.Getter; @Getter -public class JwtRefreshResponse { +public class RefreshResponseDummy { private String accessToken; private String refreshToken; } From 902058a4b03840d416de17c6387ff30c8912f177 Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Thu, 4 Apr 2024 14:16:30 +0900 Subject: [PATCH 5/5] =?UTF-8?q?Refactor:=20orElseGet=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/provider/KakaoAuthenticationProvider.java | 4 +--- .../backend/provider/NaverAuthenticationProvider.java | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java b/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java index 3b06ca7..b1a15c1 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/provider/KakaoAuthenticationProvider.java @@ -27,9 +27,7 @@ public Authentication authenticate(Authentication authentication) throws Authent throw new BadCredentialsException("유효하지 않은 OAuth 토큰입니다."); } Member member = memberRepository.findByKakaoId(kakaoId) - .orElse(null); - if (member == null) - member = memberRepository.save(Member.ofKakao(kakaoId)); + .orElseGet(() -> memberRepository.save(Member.ofKakao(kakaoId))); return UsernamePasswordAuthenticationToken.authenticated(member.getId(), null, null); } diff --git a/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java b/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java index ac59fad..a0d244b 100644 --- a/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java +++ b/daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.java @@ -28,9 +28,7 @@ public Authentication authenticate(Authentication authentication) throws Authent throw new BadCredentialsException("유효하지 않은 OAuth 토큰입니다."); } Member member = memberRepository.findByNaverId(naverId) - .orElse(null); - if (member == null) - member = memberRepository.save(Member.ofNaver(naverId)); + .orElseGet(() -> memberRepository.save(Member.ofNaver(naverId))); return UsernamePasswordAuthenticationToken.authenticated(member.getId(), null, null); }