• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

Authentication의 details 에 대해 질문 있습니다.

21.09.26 15:45 작성 조회수 1.16k

2

IpAddressFilter에서 authentication.getDetails() 부분에서 최초로 접근할 때는 details가 존재합니다.

이때는 authentication이 AnonymousAuthenticationToken 입니다.

 

그런데, 이미 인증이 된 후에는 강의에서 만든 AjaxAuthenticationToken이 쓰이는데, 여기서는 getDetails()를 하면 WebAuthenticationDetails가 null이 됩니다.

 

AjaxAuthenticationToken에서도 details를 사용하려면 어떻게 해야할까요?...

답변 1

답변을 작성해보세요.

2

네 

이 부분은 UsernamePasswordAuthenticationFileter 를 참고하시면 될 것 같습니다.

소스를 보시면

 

public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

 ------ 중략 ----

@Override

public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)

throws AuthenticationException {

if (this.postOnly && !request.getMethod().equals("POST")) {

throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());

}

String username = obtainUsername(request);

username = (username != null) ? username : "";

username = username.trim();

String password = obtainPassword(request);

password = (password != null) ? password : "";

UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);

// Allow subclasses to set the "details" property

setDetails(request, authRequest);

return this.getAuthenticationManager().authenticate(authRequest);

}

----- 중략 --------

protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {

authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));

}

위 소스처럼 필터에서 WebAuthenticationDetails 관련 로직을 추가하시면 됩니다.

이 내용은 강의에도 나와 있으니 참고하시면 됩니다.

즉 UsernamePasswordAuthenticationToken 의 setDetails 메서드에 this.authenticationDetailsSource.buildDetails(request) 의 결과값을 저장하는 것처럼 해당 필터에도 유사한 로직을 추가하시면 됩니다.

참고바랍니다.

 

 

Truestar님의 프로필

Truestar

2021.10.08

예제와 맞지않아 하다가 푹푹 빠지는 함정이 많지만 진짜 공부 잘되고있습니다^^
좋은강의 감사해요