File tree Expand file tree Collapse file tree 2 files changed +75
-0
lines changed
src/main/java/com/back/global/security Expand file tree Collapse file tree 2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .back .global .security ;
2+
3+ import com .back .global .common .dto .RsData ;
4+ import com .back .global .exception .ErrorCode ;
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import jakarta .servlet .http .HttpServletRequest ;
7+ import jakarta .servlet .http .HttpServletResponse ;
8+ import org .springframework .security .access .AccessDeniedException ;
9+ import org .springframework .security .web .access .AccessDeniedHandler ;
10+ import org .springframework .stereotype .Component ;
11+
12+ import java .io .IOException ;
13+
14+ /**
15+ * 인가 실패(403 Forbidden) 처리 클래스
16+ * - 인증은 되었으나, 권한(Role)이 부족한 경우
17+ * - Json 형태로 에러 응답을 반환
18+ */
19+ @ Component
20+ public class JwtAccessDeniedHandler implements AccessDeniedHandler {
21+ private final ObjectMapper objectMapper = new ObjectMapper ();
22+
23+ @ Override
24+ public void handle (
25+ HttpServletRequest request ,
26+ HttpServletResponse response ,
27+ AccessDeniedException accessDeniedException
28+ ) throws IOException {
29+
30+ response .setContentType ("application/json;charset=UTF-8" );
31+ response .setStatus (HttpServletResponse .SC_FORBIDDEN );
32+
33+ RsData <Void > body = RsData .fail (ErrorCode .ACCESS_DENIED );
34+
35+ response .getWriter ().write (objectMapper .writeValueAsString (body ));
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .back .global .security ;
2+
3+ import com .back .global .common .dto .RsData ;
4+ import com .back .global .exception .ErrorCode ;
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import jakarta .servlet .http .HttpServletRequest ;
7+ import jakarta .servlet .http .HttpServletResponse ;
8+ import org .springframework .security .core .AuthenticationException ;
9+ import org .springframework .security .web .AuthenticationEntryPoint ;
10+ import org .springframework .stereotype .Component ;
11+
12+ import java .io .IOException ;
13+
14+ /**
15+ * 인증 실패(401 Unauthorized) 처리 클래스
16+ * - JwtAuthenticationFilter에서 토큰이 없거나 잘못된 경우
17+ * - 인증되지 않은 사용자가 보호된 API에 접근하려는 경우
18+ * - Json 형태로 에러 응답을 반환
19+ */
20+ @ Component
21+ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
22+ private final ObjectMapper objectMapper = new ObjectMapper ();
23+
24+ @ Override
25+ public void commence (
26+ HttpServletRequest request ,
27+ HttpServletResponse response ,
28+ AuthenticationException authException
29+ ) throws IOException {
30+
31+ response .setContentType ("application/json;charset=UTF-8" );
32+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
33+
34+ RsData <Void > body = RsData .fail (ErrorCode .UNAUTHORIZED );
35+
36+ response .getWriter ().write (objectMapper .writeValueAsString (body ));
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments