728x90
반응형
SMALL
오늘 한 일
- Entity 연관관계 설정
- 서버 배포 준비
- Filter 로직 수정
작업 내용
Entity 연관관계 설정
각 팀원들이 생성한 Entity를 전체적으로 검토하고 잘못된 엔티티나 필드명이 있는지 점검하는 시간을 가졌다.
이 과정 또한 PR을 통해 코멘트를 달고 해결하는 과정이 이루어졌다. 그 과정에서 UUID, ID라는 변수명이 각 타입에 맞지 않게 생성되어있는 것을 발견하고 추가로 컨벤션을 작성하였다.
서버 배포 준비
Docker와 GitActoins를 사용한 배포 방식과 Code Deploy와 Docker를 사용한 배포 방식을 고민하게 됐다.
이전에는 Docker와 GitActions를 사용해봐서 새로운 방식으로 배포방식을 사용해볼까 고민했었다. 하지만 Code Deploy는 AWS중심의 배포 방식이라 이후에 다른 서버를 사용하게 된다면 그 과정에서 별도의 파일을 또 생성해야할까 하는 고민이 있었다. 이 고민은 이후에 추가로 공부하고 포스팅해볼 예정이다.
Filter로직 수정
이전 코드에서 관과한게 하나 있다. Authenticaion과 관련된 예외는 SpringSecurity가 감지해서 이를 처리해주지만 내가 직접 만든 CustomJwtException을 사용하게 되면 감지를 못한다는 것이다.
그래서 내가 원하는 커스텀 예외 메세지가 아닌 500 서버에러가 발생했다.
try {
String accessToken = jwtHelper.resolveToken(request);
if (accessToken != null && jwtHelper.validateToken(accessToken)) {
Authentication authentication = jwtHelper.getAuthenticationFromAccessToken(accessToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
filterChain.doFilter(request, response);
return;
}
} catch (CustomJwtException e) {
request.setAttribute("httpStatus", e.getHttpStatus());
request.setAttribute("message", e.getMessage());
request.setAttribute("code", e.getCode());
}
그래서 이 부분은 직접 출력하는 방식으로 개선했다. 직접 response에 ObjectMapper로 메세지와 코드를 담아 반환시키는 방식으로 수정했다.
try {
String accessToken = jwtHelper.resolveToken(request);
if (accessToken != null) {
jwtHelper.validateToken(accessToken);
Authentication authentication = jwtHelper.getAuthenticationFromAccessToken(accessToken);
if (authentication != null) {
SecurityContextHolder.getContext().setAuthentication(authentication);
filterChain.doFilter(request, response);
return;
}
}
} catch (CustomJwtException e) {
log.error("JWT validation failed: {}", e.getMessage());
//response에 바로 에러 응답을 설정하여 필터 체인 중단
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(e.getHttpStatus().value());
// ObjectMapper로 예쁘게 출력할 수 있도록 수정
String errorResponse = String.format("{\"status\": \"%s\", \"message\": \"%s\", \"code\": \"%s\"}",
e.getHttpStatus(), e.getMessage(), e.getCode());
response.getWriter().write(errorResponse);
return;
}
728x90
반응형
SMALL
'TIL' 카테고리의 다른 글
[TIL] Stream 활용하기, CI/CD 파이프라인 개발, 엔티티 개발 협업 (0) | 2025.02.20 |
---|---|
[TIL] 팀원간 코드 리뷰 진행 및 인덱스가 많아지면 생기는 문제, JPA의 flush 발생하는 조건 (0) | 2025.02.18 |
[TIL] SoftDelete 개발 방식, 서브 모듈 설정, PR 및 Postman으로 API문서 공유하기 (0) | 2025.02.17 |
[TIL] Spring Security 예외 설정 , S3 설정, JWT 구현, JPA Auditawre (0) | 2025.02.15 |
[TIL] 패키지 전략 및 토큰과 세션 개념 정리, 서비스와서비스끼리 의존관계를 갖게 하지 않는 이유 (1) | 2025.02.14 |