Skip to content

[종이] Chapter 8. Spring Security - Security 구조, 폼 로그인#107

Merged
issuejong merged 3 commits into
jongi/mainfrom
jongi/#106
May 26, 2026
Merged

[종이] Chapter 8. Spring Security - Security 구조, 폼 로그인#107
issuejong merged 3 commits into
jongi/mainfrom
jongi/#106

Conversation

@issuejong
Copy link
Copy Markdown
Member

✏️ 작업 내용

#️⃣ 연관된 이슈

closes #106


💡 함께 공유하고 싶은 부분

해당 주차를 공부하면서 함께 이야기하고 싶은 주제를 남겨주세요.

(어려웠던 부분과 해결 과정, 핵심 코드, 참고한 자료 등)


🤔 질문

해당 주차 워크북을 공부하면서 궁금했던 질문들을 남겨주세요.


✅ 워크북 체크리스트

  • 모든 핵심 키워드 정리를 마쳤나요?
  • 핵심 키워드에 대해 완벽히 이해하셨나요?
  • 이론 학습 이후 직접 실습을 해보는 시간을 가졌나요?
  • 미션을 수행하셨나요?
  • 미션을 기록하셨나요?

✅ 컨벤션 체크리스트

  • 디렉토리 구조 컨벤션을 잘 지켰나요?
  • pr 제목을 컨벤션에 맞게 작성하였나요?
  • pr에 해당되는 이슈를 연결하였나요?
  • 적절한 라벨을 설정하였나요?
  • 스터디원들에게 code review를 요청하기 위해 reviewer를 등록하였나요?
  • 닉네임/main 브랜치의 최신 상태를 반영하고 있는지 확인했나요?

📌 주안점

@issuejong issuejong requested a review from hhd517 May 19, 2026 13:01
@issuejong issuejong self-assigned this May 19, 2026
@issuejong issuejong requested a review from a team May 19, 2026 13:01
@issuejong issuejong added the 🚀Week 8 8주차 워크북 미션 label May 19, 2026
@issuejong issuejong changed the title Jongi/#106 [종이] Chapter 8. Spring Security - Security 구조, 폼 로그인 May 19, 2026
@hhd517
Copy link
Copy Markdown

hhd517 commented May 20, 2026

Security 설정이 깔끔하게 잘 구성된 것 같아요 !

Copy link
Copy Markdown

@DOHOON0127 DOHOON0127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다~

Comment on lines +15 to +33
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException
) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
BaseErrorCode code = GeneralErrorCode.UNAUTHORIZED;

response.setContentType("application/json;charset=UTF-8");
response.setStatus(code.getStatus().value());

ApiResponse<Void> errorResponse = ApiResponse.onFailure(code, null);
objectMapper.writeValue(response.getOutputStream(), errorResponse);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 저희가 CustomAuthenticationEntryPoint 를 만든 이유부터 생각해 보면 좋을 것 같습니다. 만약 컨트롤러나 서비스에서 예외가 발생했다면 @RestControllerAdvice를 활용해서 만들어두신 GeneralExceptionAdvice가 바로 잡아 냈을 것입니다. 그러나 시큐리티 필터에서 에러가 났다면 RestControllerAdvice가 잡아낼 수 없기 때문에 CustomAuthenticationEntryPoint 를 만들고 objectMapper로 JSON을 만들어 클라이언트에게 반환하게 해두신거죠

그런데 여기서 한 가지 아쉬운 점이 있습니다. CustomAuthenticationEntryPoint 에서 ObjectMapper로 직접 JSON 응답을 만들고 있잖아요? 이러면 에러 응답 포맷을 바꿀 때 GeneralExceptionAdvice랑 CustomAuthenticationEntryPoint 두 군데를 다 수정해야 합니다.
이걸 해결하는 방법? 좀 더 깔끔한 구조를 만드는 방법?으로 HandlerExceptionResolver를 CustomAuthenticationEntryPoint 에 주입하는 방식이 있습니다.
위에서 말씀드렸듯이 시큐리티 필터에서 에러가 났다면 RestControllerAdvice가 잡아낼 수 없기 때문에 이 방법을 사용해서 필터에서 터진 예외를 resolver.resolveException()으로 MVC 쪽에 토스해버리는 겁니다. 그러면 우리가 이미 만들어둔 @RestControllerAdvice가 그 예외를 잡아서 처리해주니까, 에러 응답 로직이 한 곳으로 모이게 됩니다.

구현은 GeneralExceptionAdvice에 AuthenticationException과 AccessDeniedException 전용 핸들러를 추가하는 방식으로 하면 될 것 같습니다!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

참고로 CustomAccessDeniedHandler도 같은 구조라서, 리팩토링할 때 같이 적용하면 될 것 같습니다~

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 반영 하겠습니다. 감사합니다~!

@issuejong issuejong merged commit aa9dc85 into jongi/main May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚀Week 8 8주차 워크북 미션

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants