인증/권한 업무 규칙과 전환 불변식 추출 #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.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Custom Authentication Entry Point - preserves error response format.
|
||||
* Maps to AUTH_001, AUTH_003, AUTH_010 error codes.
|
||||
*/
|
||||
@Component
|
||||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public CustomAuthenticationEntryPoint(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commence(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
AuthenticationException authException) throws IOException {
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
|
||||
Map<String, Object> errorResponse = Map.of(
|
||||
"code", "AUTH_001",
|
||||
"message", "Authentication required",
|
||||
"timestamp", Instant.now().toString(),
|
||||
"path", request.getRequestURI()
|
||||
);
|
||||
|
||||
objectMapper.writeValue(response.getOutputStream(), errorResponse);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue