인증/권한 업무 규칙과 전환 불변식 추출 #2
1 changed files with 46 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.example.auth.security;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom Access Denied Handler - preserves error response format.
|
||||||
|
* Maps to AUTH_030, AUTH_031, AUTH_040, AUTH_041 error codes.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
public CustomAccessDeniedHandler(ObjectMapper objectMapper) {
|
||||||
|
this.objectMapper = objectMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
AccessDeniedException accessDeniedException) throws IOException {
|
||||||
|
|
||||||
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
|
||||||
|
Map<String, Object> errorResponse = Map.of(
|
||||||
|
"code", "AUTH_030",
|
||||||
|
"message", "Insufficient permissions",
|
||||||
|
"timestamp", Instant.now().toString(),
|
||||||
|
"path", request.getRequestURI()
|
||||||
|
);
|
||||||
|
|
||||||
|
objectMapper.writeValue(response.getOutputStream(), errorResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue