• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

FilterSecurityInterceptor에서 AuthenticationManager 주입 받는 이유

23.10.06 17:12 작성 23.10.06 17:18 수정 조회수 241

0

안녕하세요 강사님

 

4) 웹 기반 인가처리 DB 연동 - FilterInvocationSecurityMetadataSource (1) 수업을 듣다가 질문드립니다.

 

FilterSecurityInterceptor에서 AuthenticationManager 주입 받는 이유가 궁금합니다.

 

인증은 앞의 필터를 지나면서 이미 완료가 되었고, FilterSecurityInterceptor는 인증이 끝난 뒤에, 권한 정보를 조회해서 체크하는 곳이고, 인증 정보는 SecurityContextHolder에서 꺼내면 되는데, AuthenticationManager로 뭘하려고 주입받는 것인지 궁금합니다.

 

AbstractSecurityInterceptor 코드를 살펴보니, authenticateIfRequired 메서드에서 SecurityContextHolder에서 꺼낸 Authentication 객체가 isAuthenticated 가 false일때 AuthenticationManager에 Authentication 객체를 넘겨서 재인증을 시도하고 있습니다.

 

private Authentication authenticateIfRequired() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.isAuthenticated() && !this.alwaysReauthenticate) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Previously Authenticated: " + authentication);
}

return authentication;
} else {
authentication = this.authenticationManager.authenticate(authentication);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Successfully Authenticated: " + authentication);
}

SecurityContextHolder.getContext().setAuthentication(authentication);
return authentication;
}
}

 

그런데, 이미 앞에서 인증 과정을 거친 결과가 isAuthenticated == false인 상태에서, 재인증을 시도하는 것이 무슨 의미가 있는지 궁금합니다. 결과가 달라지는 케이스가 있는건지, 재인증이 목적이 아닌건지... 아니면 인증을 처리하는 필터가 앞에 없을때를 가정하는건지...궁금합니다.

 

답변 주시면 감사하겠습니다.

답변 2

·

답변을 작성해보세요.

0

이 부분은 질문하신 내용처럼 이미 정해진 인증 결과를 다시 확인한다는 의미를 포함해서 어떤 원인이든간에 현재 시점에서 인증이 취소되었거나 재인증을 받아야 하는 경우가 생길 경우를 대비해서 넣은 코드라 보시면 될 것 같습니다.

사실 보안이라는 것이 기본적인 대응 부터 매우 광범위한 예기치 못한 경우를 대비해야 하는 주제이기 때문에 시작단계에서는 인증을 통과했지만 마지막 단계에서 인증이 무효인 경우가 발생할 수 있다는 가능성을 염두해 두어야 합니다.

이런 의미에서 인증이 되어 있지 못한 요청에 대한 재인증을 요구하고 있습니다.

그래서 반드시 필요한 과정이 아닌 만약 필요하다면 ( If Requried) 이라고 전제하고 있습니다.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.10.08

안녕하세요, 인프런 AI 인턴이에요.

FilterSecurityInterceptor에서 AuthenticationManager를 주입받는 이유는 재인증을 위해서입니다. FilterSecurityInterceptor는 인증이 끝난 뒤에 권한 정보를 조회하고 체크하는 역할을 합니다. 만약 SecurityContextHolder에서 꺼낸 Authentication 객체의 isAuthenticated이 false인 경우, AuthenticationManager를 통해 재인증을 시도합니다.

이는 이전에 완료된 인증 정보가 유효하지 않을 수 있으며, 재인증이 필요한 경우에 대비하는 것입니다. 이를 통해 보다 강력한 보안을 제공할 수 있습니다.

위의 코드를 보면, authenticateIfRequired 메서드에서 SecurityContextHolder에서 꺼낸 Authentication 객체가 isAuthenticated가 false인 경우에는 AuthenticationManager를 통해 재인증을 시도하고 있습니다.

따라서, FilterSecurityInterceptor에서 AuthenticationManager를 주입받는 이유는 재인증을 위해서입니다.

더 궁금한 사항이 있으시면 언제든지 물어보세요. 좋은 하루 되세요!