인증 성공 시 실행할 추가적인 로직을 작성하는 클래스
public class MemberAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Member member = (Member) authentication.getPrincipal();
// 로그인 응답 데이터
Map<String, Object> responseData = new HashMap();
responseData.put("message", "로그인 성공");
responseData.put("data", Map.of(
"email", member.getEmail(),
"nickname", member.getNickname(),
"id", member.getId()
));
// 응답 구성
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpStatus.OK.value());
// 응답 전송
ObjectMapper objectMapper = new ObjectMapper();
response.getWriter().write(objectMapper.writeValueAsString(responseData));
}
}
이 핸들러 역시 시큐리티 필터 체인에 등록해야 함
해당 문서의 CustomFilterConfigurer 적용 부분 참조
UsernamePasswordAuthenticationFilter (로그인 요청 엔트리포인트)
해당 문서의 successfulAuthentication() 부분 참조