inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 시큐리티

9) 아이피 접속 제한하기 - CustomIpAddressVoter

Ajax 로그인 방식에서 authentication 에서 details 가져올 때 NullPointerException 뜨시는 분들

273

twosom
0

@Component
@Slf4j
public class AjaxAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {

Account account = (Account) authentication.getPrincipal();
((AjaxAuthenticationToken) authentication).setDetails(new WebAuthenticationDetails(request));

response.setStatus(HttpStatus.OK.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);

/* ObjectMapper JSON 형식으로 변환해서 Response 를 이용하여 Client 에 전달 */
new ObjectMapper().writeValue(response.getWriter(), "loginOK");
}
}

위 코드처럼 AuthenticationSuccessHandler 부분에 

((AjaxAuthenticationToken) authentication).setDetails(new WebAuthenticationDetails(request));

이 코드 한줄 넣어주시면 정상적으로 작동 될거에요.

답변 0