-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from Central-MakeUs/dev
[CI/CD] dev 브랜치 최신화 반영하여 배포
- Loading branch information
Showing
5 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/com/cmc/mercury/domain/user/controller/SignOffController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.cmc.mercury.domain.user.controller; | ||
|
||
import com.cmc.mercury.domain.user.entity.User; | ||
import com.cmc.mercury.domain.user.service.SignOffService; | ||
import com.cmc.mercury.global.oauth.annotation.AuthUser; | ||
import com.cmc.mercury.global.response.SuccessResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/signoff") | ||
@RequiredArgsConstructor | ||
@Tag(name = "SignOffController", description = "로그아웃 & 탈퇴 API") | ||
public class SignOffController { | ||
|
||
private final SignOffService signOffService; | ||
|
||
@PostMapping("/logout") | ||
@Operation(summary = "로그아웃", | ||
description = "로그인한 사용자의 refresh token을 무효화시킵니다.(호출 후 프론트측의 access token을 무력화시켜주세요!)") | ||
public SuccessResponse<User> logout(@AuthUser User user, HttpServletRequest request, HttpServletResponse response) { | ||
|
||
User logoutUser = signOffService.logout(user); | ||
clearAuthContext(request, response); | ||
|
||
return SuccessResponse.ok(logoutUser); | ||
} | ||
|
||
@PostMapping("/withdraw") | ||
@Operation(summary = "회원탈퇴(soft delete)", | ||
description = "로그인한 사용자의 refresh token을 무효화시키며 상태를 INAVTIVE로 바꿉니다.(호출 후 프론트측의 access token을 무력화시켜주세요!)") | ||
public SuccessResponse<User> withdraw(@AuthUser User user, HttpServletRequest request, HttpServletResponse response) { | ||
|
||
User deletedUser = signOffService.withdraw(user); | ||
clearAuthContext(request, response); | ||
|
||
return SuccessResponse.ok(deletedUser); | ||
} | ||
|
||
private void clearAuthContext(HttpServletRequest request, HttpServletResponse response) { | ||
|
||
// HttpSession & SecurityContext 초기화 | ||
request.getSession().invalidate(); // 세션 초기화 | ||
SecurityContextHolder.clearContext(); // Spring Security 인증 정보 제거 | ||
|
||
// HttpOnly 쿠키 제거 | ||
Cookie cookie = new Cookie("refreshToken", null); | ||
cookie.setHttpOnly(true); | ||
// cookie.setSecure(true); | ||
cookie.setPath("/"); | ||
cookie.setMaxAge(0); // 쿠키 즉시 삭제 | ||
response.addCookie(cookie); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/cmc/mercury/domain/user/service/SignOffService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.cmc.mercury.domain.user.service; | ||
|
||
import com.cmc.mercury.domain.user.entity.User; | ||
import com.cmc.mercury.domain.user.entity.UserStatus; | ||
import com.cmc.mercury.global.exception.CustomException; | ||
import com.cmc.mercury.global.exception.ErrorCode; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SignOffService { | ||
|
||
@Transactional | ||
public User logout(User user) { | ||
|
||
// DB에서 refresh token 제거 | ||
user.updateRefreshToken(null); | ||
|
||
return user; | ||
} | ||
|
||
@Transactional | ||
public User withdraw(User user) { | ||
|
||
if (user.getUserStatus() == UserStatus.INACTIVE) { | ||
throw new CustomException(ErrorCode.ALREADY_WITHDRAWN); | ||
} | ||
|
||
user.deleteUser(); | ||
user.updateRefreshToken(null); // Refresh Token 삭제 | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters