-
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 #16 from Leets-Official/10-fix-리프레시-토큰-수정-및-로그아웃
10 fix 리프레시 토큰 수정 및 로그아웃
- Loading branch information
Showing
11 changed files
with
145 additions
and
45 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/leets/commitatobe/domain/user/presentation/AuthController.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,42 @@ | ||
package com.leets.commitatobe.domain.user.presentation; | ||
|
||
import com.leets.commitatobe.domain.login.presentation.dto.JwtResponse; | ||
import com.leets.commitatobe.domain.user.usecase.AuthService; | ||
import com.leets.commitatobe.global.response.ApiResponse; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
//로그아웃 | ||
@PostMapping("/logout") | ||
public ApiResponse<Void> logout(HttpServletResponse response){ | ||
//리프레시 토큰 쿠키 삭제 | ||
Cookie myCookie = new Cookie("refreshToken", null); | ||
myCookie.setMaxAge(0); | ||
myCookie.setPath("/"); | ||
response.addCookie(myCookie); | ||
|
||
return ApiResponse.onSuccess(null); | ||
} | ||
|
||
//액세스 토큰 리프레시 | ||
@PostMapping("/refresh") | ||
public ApiResponse<Object> refreshAccessToken(HttpServletRequest request, HttpServletResponse response) { | ||
//리프레시 토큰을 통한 액세스 토큰 갱신 | ||
JwtResponse jwt = authService.regenerateAccessToken(request, response); | ||
// 액세스 토큰을 헤더에 설정 | ||
response.setHeader("Authentication", "Bearer " + jwt.accessToken()); | ||
|
||
return ApiResponse.onSuccess(jwt); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/leets/commitatobe/domain/user/usecase/AuthService.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,44 @@ | ||
package com.leets.commitatobe.domain.user.usecase; | ||
|
||
import com.leets.commitatobe.domain.login.presentation.dto.JwtResponse; | ||
import com.leets.commitatobe.global.exception.ApiException; | ||
import com.leets.commitatobe.global.response.code.status.ErrorStatus; | ||
import com.leets.commitatobe.global.utils.JwtProvider; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
|
||
private final JwtProvider jwtProvider; | ||
|
||
public JwtResponse regenerateAccessToken(HttpServletRequest request, HttpServletResponse response){ | ||
//쿠키에서 리프레시 토큰 찾기 | ||
String refreshToken = null; | ||
Cookie[] cookies = request.getCookies(); | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals("refreshToken")) { | ||
refreshToken = cookie.getValue(); | ||
} | ||
} | ||
|
||
try { | ||
jwtProvider.validateToken(refreshToken); | ||
String githubId = jwtProvider.getGithubIdFromToken(refreshToken); | ||
return jwtProvider.regenerateTokenDto(githubId, refreshToken); | ||
} | ||
catch (Exception e) { | ||
//리프레시 토큰 쿠키 삭제 | ||
Cookie myCookie = new Cookie("refreshToken", null); | ||
myCookie.setMaxAge(0); | ||
myCookie.setPath("/"); | ||
response.addCookie(myCookie); | ||
throw new ApiException(ErrorStatus._REFRESH_TOKEN_EXPIRED); | ||
} | ||
|
||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
spring: | ||
profiles: | ||
default: dev | ||
default: local |