-
Notifications
You must be signed in to change notification settings - Fork 1
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 #28 from KNU-HAEDAL-Website/feat-exeption-login-is…
…sue-22 Feat: 로그인, 로그아웃, reissue 예외처리
- Loading branch information
Showing
19 changed files
with
326 additions
and
265 deletions.
There are no files selected for viewing
Binary file not shown.
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
83 changes: 6 additions & 77 deletions
83
src/main/java/com/haedal/haedalweb/controller/ReissueController.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 |
---|---|---|
@@ -1,95 +1,24 @@ | ||
package com.haedal.haedalweb.controller; | ||
|
||
import com.haedal.haedalweb.constants.LoginConstants; | ||
import com.haedal.haedalweb.jwt.JWTUtil; | ||
import com.haedal.haedalweb.service.RedisService; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import jakarta.servlet.http.Cookie; | ||
import com.haedal.haedalweb.constants.SuccessCode; | ||
import com.haedal.haedalweb.service.IssueService; | ||
import com.haedal.haedalweb.util.ResponseUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class ReissueController { | ||
|
||
private final JWTUtil jwtUtil; | ||
private final RedisService redisService; | ||
private final IssueService issueService; | ||
|
||
@PostMapping("/reissue") | ||
public ResponseEntity<?> reissue(HttpServletRequest request, HttpServletResponse response) { | ||
//get refresh token | ||
String refreshToken = null; | ||
Cookie[] cookies = request.getCookies(); | ||
|
||
for (Cookie cookie : cookies) { | ||
|
||
if (cookie.getName().equals(LoginConstants.REFRESH_TOKEN)) { | ||
|
||
refreshToken = cookie.getValue(); | ||
} | ||
} | ||
|
||
if (refreshToken == null) { | ||
|
||
//response status code | ||
return ResponseEntity.badRequest().body(LoginConstants.REFRESH_TOKEN_NULL); | ||
} | ||
|
||
//expired check | ||
try { | ||
jwtUtil.isExpired(refreshToken); | ||
} catch (ExpiredJwtException e) { | ||
|
||
//response status code | ||
return ResponseEntity.badRequest().body(LoginConstants.REFRESH_TOKEN_EXPIRED); | ||
} | ||
|
||
// 토큰이 refresh인지 확인 (발급시 페이로드에 명시) | ||
String category = jwtUtil.getCategory(refreshToken); | ||
|
||
if (!category.equals(LoginConstants.REFRESH_TOKEN)) { | ||
|
||
//response status code | ||
return ResponseEntity.badRequest().body(LoginConstants.INVALID_REFRESH_TOKEN); | ||
} | ||
|
||
boolean isExist = redisService.existsByRefreshToken(refreshToken); | ||
|
||
if (!isExist) { | ||
|
||
//response body | ||
return new ResponseEntity<>("invalid refresh token", HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
String userId = jwtUtil.getUserId(refreshToken); | ||
String role = jwtUtil.getRole(refreshToken); | ||
//make new JWT | ||
String newAccessToken = jwtUtil.createJwt(LoginConstants.ACCESS_TOKEN, userId, role, LoginConstants.ACCESS_TOKEN_EXPIRATION_TIME_MS); | ||
String newRefreshToken = jwtUtil.createJwt(LoginConstants.REFRESH_TOKEN, userId, role, LoginConstants.REFRESH_TOKEN_EXPIRATION_TIME_MS); | ||
|
||
redisService.deleteRefreshToken(refreshToken); | ||
redisService.saveRefreshToken(newRefreshToken, userId); | ||
|
||
//response | ||
response.setHeader(LoginConstants.ACCESS_TOKEN, newAccessToken); | ||
response.addCookie(createCookie(LoginConstants.REFRESH_TOKEN, newRefreshToken)); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
private Cookie createCookie(String key, String value) { | ||
|
||
Cookie cookie = new Cookie(key, value); | ||
cookie.setMaxAge((int)LoginConstants.REFRESH_TOKEN_EXPIRATION_TIME_S); | ||
//cookie.setSecure(true); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
issueService.reissueToken(request, response); | ||
|
||
return cookie; | ||
return ResponseUtil.buildSuccessResponseEntity(SuccessCode.REISSUE_SUCCESS); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/haedal/haedalweb/exception/FilterExceptionHandler.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,39 @@ | ||
package com.haedal.haedalweb.exception; | ||
|
||
import com.haedal.haedalweb.constants.ErrorCode; | ||
import com.haedal.haedalweb.dto.ErrorResponse; | ||
import com.haedal.haedalweb.util.ResponseUtil; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
import java.io.IOException; | ||
|
||
public class FilterExceptionHandler extends GenericFilterBean { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
this.doFilter((HttpServletRequest)request, (HttpServletResponse)response, chain); | ||
} | ||
|
||
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
try { | ||
chain.doFilter(request, response); | ||
} catch (BusinessException e) { | ||
sendErrorResponse(response, e); | ||
} | ||
} | ||
|
||
private void sendErrorResponse(HttpServletResponse response, BusinessException e) { | ||
ErrorCode errorCode = e.getErrorCode(); | ||
response.setStatus(errorCode.getHttpStatus().value()); | ||
ErrorResponse errorResponse = ErrorResponse.builder() | ||
.message(errorCode.getMessage()) | ||
.build(); | ||
|
||
ResponseUtil.writeAsJsonResponse(response, errorResponse); | ||
} | ||
} |
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
Oops, something went wrong.