content: package com.codeages.termiusplus.security;
  
  import com.codeages.termiusplus.biz.ErrorCode;
  import com.codeages.termiusplus.exception.AppError;
  import com.codeages.termiusplus.exception.AppException;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.http.MediaType;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.web.AuthenticationEntryPoint;
  
  import jakarta.servlet.ServletException;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  public class AuthEntryPoint implements AuthenticationEntryPoint {
  
      private final ObjectMapper objectMapper;
  
      public AuthEntryPoint(ObjectMapper objectMapper) {
          this.objectMapper = objectMapper;
      }
  
      @Override
      public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
          var error = AppError.fromAppException(new AppException(ErrorCode.UNAUTHORIZED), request);
          response.setCharacterEncoding("UTF-8");
          response.setContentType(MediaType.APPLICATION_JSON_VALUE);
          response.setStatus(error.getStatus());
          response.getWriter().write(objectMapper.writeValueAsString(error));
      }
  }
  