From cdd7573027fb3ef2ae30430c7e8a7f67f91e3e20 Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 21:17:36 +0900 Subject: [PATCH 1/7] =?UTF-8?q?Feat:=20getLoginType=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/elice/ustory/global/jwt/JwtUtil.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/elice/ustory/global/jwt/JwtUtil.java b/src/main/java/com/elice/ustory/global/jwt/JwtUtil.java index 8c77a48..340c1ea 100644 --- a/src/main/java/com/elice/ustory/global/jwt/JwtUtil.java +++ b/src/main/java/com/elice/ustory/global/jwt/JwtUtil.java @@ -75,6 +75,12 @@ public Long getUserPk(String token) { .parseClaimsJws(token).getBody().get("userId").toString()); } + public String getLoginType(String token) { + log.info("[getLoginType] 현재 로그인 된 유저의 로그인 방식 추출"); + return Jwts.parserBuilder().setSigningKey(jwtTokenProvider.getSecretKey()).build() + .parseClaimsJws(token).getBody().get("loginType").toString(); + } + public boolean validateToken(String jwtToken) { log.info("[validateToken] 토큰 유효 체크 시작 "); try { From 6d001f599f4b853e48816c52e03182719d09d6fe Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 22:06:11 +0900 Subject: [PATCH 2/7] =?UTF-8?q?Fix:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83?= =?UTF-8?q?=20=ED=86=B5=ED=95=A9=EC=9D=84=20=EC=9C=84=ED=95=9C=20Kakao=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20=EB=A9=94=EC=86=8C?= =?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 --- .../global/oauth/kakao/KakaoController.java | 16 ---------------- .../ustory/global/oauth/kakao/KakaoService.java | 10 +--------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoController.java b/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoController.java index 802face..7ebac03 100644 --- a/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoController.java +++ b/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoController.java @@ -1,7 +1,6 @@ package com.elice.ustory.global.oauth.kakao; import com.elice.ustory.domain.user.dto.LoginResponse; -import com.elice.ustory.domain.user.dto.LogoutResponse; import com.elice.ustory.domain.user.service.UserService; import com.elice.ustory.global.exception.dto.ErrorResponse; import io.swagger.v3.oas.annotations.Operation; @@ -10,14 +9,11 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -57,16 +53,4 @@ public ResponseEntity kakaoLogin(@RequestParam String code, HttpS log.info("[kakaoLogin] 카카오 닉네임: {}", nickname); return ResponseEntity.ok().body(loginResponse); } - - @Operation(summary = "KAKAO LOGOUT API", description = "카카오 로그아웃") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "Ok", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LogoutResponse.class))), - @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))), - @ApiResponse(responseCode = "500", description = "Internal Server Error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))) - }) - @RequestMapping(value = "/auth/logout/kakao", method = {RequestMethod.GET, RequestMethod.POST}) - public ResponseEntity kakaoLogout(HttpServletRequest request) { - LogoutResponse logoutResponse = kakaoService.kakaoLogout(request); - return ResponseEntity.ok().body(logoutResponse); - } } diff --git a/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoService.java b/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoService.java index 8155317..3f22810 100644 --- a/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoService.java +++ b/src/main/java/com/elice/ustory/global/oauth/kakao/KakaoService.java @@ -8,17 +8,14 @@ import com.elice.ustory.domain.diaryUser.entity.DiaryUserId; import com.elice.ustory.domain.diaryUser.repository.DiaryUserRepository; import com.elice.ustory.domain.user.dto.LoginResponse; -import com.elice.ustory.domain.user.dto.LogoutResponse; import com.elice.ustory.domain.user.entity.Users; import com.elice.ustory.domain.user.repository.UserRepository; -import com.elice.ustory.domain.user.service.UserService; import com.elice.ustory.global.exception.model.NotFoundException; import com.elice.ustory.global.jwt.JwtTokenProvider; import com.elice.ustory.global.jwt.JwtUtil; import com.elice.ustory.global.redis.kakao.KakaoTokenService; import com.elice.ustory.global.redis.refresh.RefreshTokenService; import com.elice.ustory.global.util.RandomGenerator; -import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -39,7 +36,6 @@ public class KakaoService { private final KakaoTokenService kakaoTokenService; private final JwtUtil jwtUtil; private final KakaoOauth kakaoOauth; - private final UserService userService; private final RandomGenerator randomGenerator; private final PasswordEncoder passwordEncoder; @@ -93,13 +89,9 @@ public LoginResponse kakaoLogin(String kakaoUserId, HttpServletResponse response return loginResponse; } - public LogoutResponse kakaoLogout(HttpServletRequest request) { - String accessToken = jwtUtil.getTokenFromRequest(request); + public void kakaoLogout(String accessToken) { String kakaoToken = jwtUtil.getSocialToken(accessToken); kakaoOauth.expireKakaoToken(kakaoToken); kakaoTokenService.removeKakaoTokenInfo(accessToken); - userService.logout(request); - - return LogoutResponse.builder().success(true).build(); } } From 85a9c57f3640821a28bf30935ca4fb405133b156 Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 22:06:22 +0900 Subject: [PATCH 3/7] =?UTF-8?q?Fix:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83?= =?UTF-8?q?=20=ED=86=B5=ED=95=A9=EC=9D=84=20=EC=9C=84=ED=95=9C=20Naver=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20=EB=A9=94=EC=86=8C?= =?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 --- .../ustory/global/oauth/naver/NaverController.java | 12 ------------ .../ustory/global/oauth/naver/NaverService.java | 10 +--------- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/main/java/com/elice/ustory/global/oauth/naver/NaverController.java b/src/main/java/com/elice/ustory/global/oauth/naver/NaverController.java index a2fb0d5..0522815 100644 --- a/src/main/java/com/elice/ustory/global/oauth/naver/NaverController.java +++ b/src/main/java/com/elice/ustory/global/oauth/naver/NaverController.java @@ -60,16 +60,4 @@ public ResponseEntity naverLogin(@RequestParam(name = "code") Str log.info("[naverLogin] 네이버 닉네임: {}", nickname); return ResponseEntity.ok().body(loginResponse); } - - @Operation(summary = "NAVER LOGOUT API", description = "네이버 로그아웃") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "Ok", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LogoutResponse.class))), - @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))), - @ApiResponse(responseCode = "500", description = "Internal Server Error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))) - }) - @RequestMapping(value = "/auth/logout/naver", method = {RequestMethod.GET, RequestMethod.POST}) - public ResponseEntity naverLogout(HttpServletRequest request) { - LogoutResponse logoutResponse = naverService.naverLogout(request); - return ResponseEntity.ok().body(logoutResponse); - } } diff --git a/src/main/java/com/elice/ustory/global/oauth/naver/NaverService.java b/src/main/java/com/elice/ustory/global/oauth/naver/NaverService.java index 8aa5836..f291d26 100644 --- a/src/main/java/com/elice/ustory/global/oauth/naver/NaverService.java +++ b/src/main/java/com/elice/ustory/global/oauth/naver/NaverService.java @@ -8,7 +8,6 @@ import com.elice.ustory.domain.diaryUser.entity.DiaryUserId; import com.elice.ustory.domain.diaryUser.repository.DiaryUserRepository; import com.elice.ustory.domain.user.dto.LoginResponse; -import com.elice.ustory.domain.user.dto.LogoutResponse; import com.elice.ustory.domain.user.entity.Users; import com.elice.ustory.domain.user.repository.UserRepository; import com.elice.ustory.domain.user.service.UserService; @@ -18,7 +17,6 @@ import com.elice.ustory.global.redis.naver.NaverTokenService; import com.elice.ustory.global.redis.refresh.RefreshTokenService; import com.elice.ustory.global.util.RandomGenerator; -import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -34,11 +32,9 @@ public class NaverService { private final UserRepository userRepository; private final DiaryRepository diaryRepository; private final DiaryUserRepository diaryUserRepository; - private final UserService userService; private final RefreshTokenService refreshTokenService; private final NaverTokenService naverTokenService; private final JwtTokenProvider jwtTokenProvider; - private final JwtUtil jwtUtil; private final RandomGenerator randomGenerator; private final PasswordEncoder passwordEncoder; @@ -92,11 +88,7 @@ public LoginResponse naverLogin(String naverEmail, HttpServletResponse response, return loginResponse; } - public LogoutResponse naverLogout(HttpServletRequest request) { - String accessToken = jwtUtil.getTokenFromRequest(request); + public void naverLogout(String accessToken) { naverTokenService.removeNaverTokenInfo(accessToken); - userService.logout(request); - - return LogoutResponse.builder().success(true).build(); } } From ac5389a73adb723d2c54938b49f7b9d1c15467c9 Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 22:06:53 +0900 Subject: [PATCH 4/7] =?UTF-8?q?Fix:=20Basic=20&=20Social=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=95=84=EC=9B=83=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 17 ++++++++++++++++- .../domain/user/service/UserService.java | 19 +++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java index ff4377f..6079ef6 100644 --- a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java +++ b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java @@ -8,6 +8,9 @@ import com.elice.ustory.domain.user.service.UserService; import com.elice.ustory.global.exception.dto.ErrorResponse; import com.elice.ustory.global.jwt.JwtAuthorization; +import com.elice.ustory.global.jwt.JwtUtil; +import com.elice.ustory.global.oauth.kakao.KakaoService; +import com.elice.ustory.global.oauth.naver.NaverService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -32,6 +35,9 @@ public class UserController { private final UserService userService; private final EmailService emailService; + private final KakaoService kakaoService; + private final NaverService naverService; + private final JwtUtil jwtUtil; @Operation(summary = "Create User API", description = "기본 회원가입 후 유저를 생성한다." + "
비밀번호는 **숫자, 영문, 특수문자 각 1개를 포함한 8~16자** 이며," + @@ -98,7 +104,16 @@ public ResponseEntity loginBasic(@Valid @RequestBody LoginRequest }) @PostMapping("/logout") public ResponseEntity logoutBasic(HttpServletRequest request) { - LogoutResponse logoutResponse = userService.logout(request); + String accessToken = jwtUtil.getTokenFromRequest(request); + String loginType = jwtUtil.getLoginType(accessToken); + + if(loginType.equals("KAKAO")){ + kakaoService.kakaoLogout(accessToken); + }else if(loginType.equals("NAVER")){ + naverService.naverLogout(accessToken); + } + + LogoutResponse logoutResponse = userService.logout(accessToken); return ResponseEntity.ok().body(logoutResponse); } diff --git a/src/main/java/com/elice/ustory/domain/user/service/UserService.java b/src/main/java/com/elice/ustory/domain/user/service/UserService.java index 005da61..2a0ee9c 100644 --- a/src/main/java/com/elice/ustory/domain/user/service/UserService.java +++ b/src/main/java/com/elice/ustory/domain/user/service/UserService.java @@ -14,8 +14,10 @@ import com.elice.ustory.domain.user.repository.UserRepository; import com.elice.ustory.global.exception.model.*; import com.elice.ustory.global.jwt.JwtTokenProvider; +import com.elice.ustory.global.oauth.kakao.KakaoService; +import com.elice.ustory.global.oauth.naver.NaverService; +import com.elice.ustory.global.redis.naver.NaverTokenService; import com.elice.ustory.global.redis.refresh.RefreshTokenService; -import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; @@ -226,19 +228,8 @@ public LoginResponse login(LoginRequest loginRequest, HttpServletResponse respon return loginResponse; } - public LogoutResponse logout(HttpServletRequest request) { - // 리프레시 토큰 삭제 - String token = request.getHeader("Authorization"); - - if (token == null) { - throw new UnauthorizedException(UserMessageConstants.UNAUTHORIZED_MESSAGE); - } - if (token.startsWith("Bearer ")) { - token = token.substring(7); - } - - refreshTokenService.removeTokenInfo(token); - + public LogoutResponse logout(String accessToken) { + refreshTokenService.removeTokenInfo(accessToken); LogoutResponse logoutResponse = LogoutResponse.builder().success(true).build(); return logoutResponse; } From e82295ba7a4c07cffce5c9912d329ec420cff718 Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 22:31:55 +0900 Subject: [PATCH 5/7] =?UTF-8?q?Fix:=20Basic=20Login=20=EC=95=A1=EC=84=B8?= =?UTF-8?q?=EC=8A=A4=20=ED=86=A0=ED=81=B0=EC=97=90=20LoginType=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=A3=BC=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/elice/ustory/global/jwt/JwtTokenProvider.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/elice/ustory/global/jwt/JwtTokenProvider.java b/src/main/java/com/elice/ustory/global/jwt/JwtTokenProvider.java index 49b49d4..571ec68 100644 --- a/src/main/java/com/elice/ustory/global/jwt/JwtTokenProvider.java +++ b/src/main/java/com/elice/ustory/global/jwt/JwtTokenProvider.java @@ -35,6 +35,7 @@ public String createAccessToken(Long userId) { Claims claims = Jwts.claims(); Date now = new Date(); claims.put("userId", userId); + claims.put("loginType", Users.LoginType.BASIC); log.info("[createAccessToken] access 토큰 생성 완료"); return Jwts.builder() .setClaims(claims) From 7e7721f2b9be19c8ad1f45a81cac63f7761bec9d Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 22:45:08 +0900 Subject: [PATCH 6/7] =?UTF-8?q?Fix:=20LogoutResponse=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=EA=B0=92=EC=97=90=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/controller/UserController.java | 2 +- .../ustory/domain/user/dto/LogoutResponse.java | 16 +++++++++++----- .../ustory/domain/user/service/UserService.java | 5 ++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java index 6079ef6..6b6616e 100644 --- a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java +++ b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java @@ -113,7 +113,7 @@ public ResponseEntity logoutBasic(HttpServletRequest request) { naverService.naverLogout(accessToken); } - LogoutResponse logoutResponse = userService.logout(accessToken); + LogoutResponse logoutResponse = userService.logout(accessToken, loginType); return ResponseEntity.ok().body(logoutResponse); } diff --git a/src/main/java/com/elice/ustory/domain/user/dto/LogoutResponse.java b/src/main/java/com/elice/ustory/domain/user/dto/LogoutResponse.java index 97e8793..5f997e7 100644 --- a/src/main/java/com/elice/ustory/domain/user/dto/LogoutResponse.java +++ b/src/main/java/com/elice/ustory/domain/user/dto/LogoutResponse.java @@ -1,10 +1,16 @@ package com.elice.ustory.domain.user.dto; -import lombok.Builder; -import lombok.Data; +import lombok.Getter; +import lombok.RequiredArgsConstructor; -@Data -@Builder +@RequiredArgsConstructor +@Getter public class LogoutResponse { - Boolean success; + private Boolean success; + private String loginType; + + public LogoutResponse(Boolean success, String loginType) { + this.success = success; + this.loginType = loginType; + } } diff --git a/src/main/java/com/elice/ustory/domain/user/service/UserService.java b/src/main/java/com/elice/ustory/domain/user/service/UserService.java index 2a0ee9c..bdf56c1 100644 --- a/src/main/java/com/elice/ustory/domain/user/service/UserService.java +++ b/src/main/java/com/elice/ustory/domain/user/service/UserService.java @@ -228,10 +228,9 @@ public LoginResponse login(LoginRequest loginRequest, HttpServletResponse respon return loginResponse; } - public LogoutResponse logout(String accessToken) { + public LogoutResponse logout(String accessToken, String loginType) { refreshTokenService.removeTokenInfo(accessToken); - LogoutResponse logoutResponse = LogoutResponse.builder().success(true).build(); - return logoutResponse; + return new LogoutResponse(true, loginType); } public MyPageResponse showMyPage(Long userId) { From 6ad200bbdc80a4261d59353a4e2cac0f6b448067 Mon Sep 17 00:00:00 2001 From: wambatcode Date: Mon, 8 Jul 2024 23:19:39 +0900 Subject: [PATCH 7/7] =?UTF-8?q?Refactor:=20Kakao=20=EB=B0=8F=20Naver=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=83=80=EC=9E=85=20=EC=83=81?= =?UTF-8?q?=EC=88=98=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ustory/domain/user/controller/UserController.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java index 6b6616e..d7aa63e 100644 --- a/src/main/java/com/elice/ustory/domain/user/controller/UserController.java +++ b/src/main/java/com/elice/ustory/domain/user/controller/UserController.java @@ -39,6 +39,9 @@ public class UserController { private final NaverService naverService; private final JwtUtil jwtUtil; + private static final String KAKAO_LOGIN_TYPE = "KAKAO"; + private static final String NAVER_LOGIN_TYPE = "NAVER"; + @Operation(summary = "Create User API", description = "기본 회원가입 후 유저를 생성한다." + "
비밀번호는 **숫자, 영문, 특수문자 각 1개를 포함한 8~16자** 이며," + "
보안을 위해, 이때 특수문자는 **~!@#%^*** 만 허용한다.") @@ -107,9 +110,9 @@ public ResponseEntity logoutBasic(HttpServletRequest request) { String accessToken = jwtUtil.getTokenFromRequest(request); String loginType = jwtUtil.getLoginType(accessToken); - if(loginType.equals("KAKAO")){ + if(loginType.equals(KAKAO_LOGIN_TYPE)){ kakaoService.kakaoLogout(accessToken); - }else if(loginType.equals("NAVER")){ + }else if(loginType.equals(NAVER_LOGIN_TYPE)){ naverService.naverLogout(accessToken); }