fo bo 머지작업 - 오류수정

This commit is contained in:
jazz
2025-11-28 10:27:51 +09:00
parent c7fd1750dd
commit 03a5491ed2
14 changed files with 109 additions and 36 deletions

View File

@@ -4,19 +4,31 @@ import com.wb.domainserver.exception.code.ErrCode;
import com.wb.domainserver.exception.WnbtException;
import com.wb.domainserver.exception.WnbtRestException;
import com.wb.domainserver.exception.WnbtWebClientException;
import com.wb.domainserver.realm.RealmContextHolder;
import com.wb.domainserver.response.WnbtResponse;
import com.wb.domainserver.response.WnbtResponseBody;
import com.wb.domainserver.util.CookieUtils;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.ClientAuthorizationRequiredException;
import org.springframework.util.MethodInvoker;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.Map;
@Slf4j
@ControllerAdvice
public class GlobalExceptionControllerAdvice {
@@ -35,14 +47,25 @@ public class GlobalExceptionControllerAdvice {
}
@ExceptionHandler(value = WnbtRestException.class)
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<WnbtResponse> ajaxErrorHandler(HttpServletRequest req, HttpServletResponse res, WnbtRestException e){
WnbtResponse wnbtResponse = WnbtResponse.builder()
.statusCode(res.getStatus())
.requestUrl(String.valueOf(req.getRequestURL()))
.resBody(new WnbtResponseBody(e.getMessage()))
.resBody(new WnbtResponseBody(e, e.getMessage()))
.build();
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(wnbtResponse);
int errCode = 500;
if(StringUtils.hasText(e.getCode())){
errCode = Integer.parseInt(e.getCode());
}
return ResponseEntity
.status(errCode)
.contentType(MediaType.APPLICATION_JSON)
.body(wnbtResponse);
// return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(wnbtResponse);
//422 : 서버가 요청을 이해하고 요청 문법도 올바르지만 요청된 지시를 따를 수 없음을 나타냄
//요청 데이터에 대해 validation 처리를 하면서 적합하지 않은 데이터를 받았을 때 반환
}
@@ -82,4 +105,25 @@ public class GlobalExceptionControllerAdvice {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(wnbtResponse);
}
/** 해당 exception handler에 오는 경우 토큰 변조로 간주하고
* 로그아웃 처리를 위해 로그인 관련 쿠키 및 logout으로 redirect 처리하여
* 재로그인 유도
*/
@ExceptionHandler(value = JwtException.class)
public void JwtException(HttpServletRequest request, HttpServletResponse response, JwtException e) throws IOException {
log.error(e.getMessage(), e);
String realm = RealmContextHolder.getRealm().toLowerCase();
Map<String, String> cookiePath = CookieUtils.loginCookiePath();
//리프레쉬 토큰 삭제
CookieUtils.deleteCookie("RFT", cookiePath.get("RFTPath"), request, response);
//엑세스토큰 삭제
CookieUtils.deleteCookie("ACT", cookiePath.get("ACTPath"), request, response);
//토큰만료 검증쿠키 삭제
CookieUtils.deleteCookie("token_expire", cookiePath.get("ACTPath"), request, response);
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.sendRedirect("/"+realm+"/logout");
}
}