Skip to content

Commit b602d5f

Browse files
committed
feat: Security FilterChain 예외 처리 구현
- ErrorResponse 타입으로 예외 응답
1 parent ea79cc6 commit b602d5f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dmu.dasom.api.global.auth.handler;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import dmu.dasom.api.domain.common.exception.ErrorCode;
5+
import dmu.dasom.api.domain.common.exception.ErrorResponse;
6+
import jakarta.servlet.http.HttpServletRequest;
7+
import jakarta.servlet.http.HttpServletResponse;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.security.access.AccessDeniedException;
11+
import org.springframework.security.web.access.AccessDeniedHandler;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.io.IOException;
15+
16+
@Component
17+
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
18+
19+
private final ObjectMapper objectMapper;
20+
21+
public AccessDeniedHandlerImpl(ObjectMapper objectMapper) {
22+
this.objectMapper = objectMapper;
23+
}
24+
25+
@Override
26+
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
27+
response.setStatus(HttpStatus.FORBIDDEN.value());
28+
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
29+
response.setCharacterEncoding("UTF-8");
30+
response.getWriter().write(objectMapper.writeValueAsString(new ErrorResponse(ErrorCode.FORBIDDEN)));
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dmu.dasom.api.global.auth.handler;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import dmu.dasom.api.domain.common.exception.ErrorCode;
5+
import dmu.dasom.api.domain.common.exception.ErrorResponse;
6+
import jakarta.servlet.http.HttpServletRequest;
7+
import jakarta.servlet.http.HttpServletResponse;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.security.core.AuthenticationException;
11+
import org.springframework.security.web.AuthenticationEntryPoint;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.io.IOException;
15+
16+
@Component
17+
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
18+
19+
private final ObjectMapper objectMapper;
20+
21+
public AuthenticationEntryPointImpl(ObjectMapper objectMapper) {
22+
this.objectMapper = objectMapper;
23+
}
24+
25+
@Override
26+
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
27+
response.setStatus(HttpStatus.UNAUTHORIZED.value());
28+
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
29+
response.setCharacterEncoding("UTF-8");
30+
response.getWriter().write(objectMapper.writeValueAsString(new ErrorResponse(ErrorCode.UNAUTHORIZED)));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)