• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

WebSecruityConfigurerAdapter가 2개이면 아래 검은 노드로 표현된 filter들이 2개씩 있는 것인가요?

22.04.03 22:46 작성 조회수 104

0

안녕하세요 좋은 강의 감사하게 잘 보고 있습니다.
시큐리티 공부를 해야하는 중 이런 강의가 있어서 다행이라고 생각하고 있습니다.

다름이 아니라 WebSecruityConfigurerAdapter 설정을 강의에서와 같이 2개 만들면 아래쪽 검은 노드로 표현된 filter들이 최대 2개씩 존재할 수 있다고 생각하면 되는지 궁금합니다.

만약 그렇다면 우선순위가 높은 filters에서 Authentication 객체가 할당된 경우 우선순위가 낮은 filters에서는 어떻게 되는지요. 예를 들어서 SecurityContextHolder에 이미 Authentication 객체가 존재하는데 만약에 우선순위가 낮은 filters에서도 UsernamePasswordAuthenticationFilter가 있어서 새롭게 인증이 되게 되면 같은 일을 반복하는 것 같기도 하고 무언가 문제가 생길 것 같기도 해서요.

답변 1

답변을 작성해보세요.

1

네 

강의에서도 설명하고 있지만 스프링 시큐리티 설정을 여러개 정의하면 각 필터들이 구성되는데 그 필터에서 생성되고 실행되는 모든 객체는 서로간의 간섭없이 진행됩니다.

SecurityContextHolder 에 저장되어 있는 SecurityContext 객체도 각 설정마다 새롭게 생성되고 인증처리가 되기 때문에 문제될 소지는 없습니다.

물론 세션 자체는 공유를 하겠지만 동일한 사용자가 각 다른 설정에서 생성된 UsernamePasswordAuthenticationFilter 를 통해 여러번 인증을 받더라도 먼저 인증처리된 SecurityContextHolder 의 SecurityContext 를 삭제하고 다시 SecurityContext 를 생성 및 저장하기 때문에 중복으로 인한 문제도 발생하지 않습니다.

protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authResult);
SecurityContextHolder.setContext(context);
if (this.logger.isDebugEnabled()) {
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authResult));
}
this.rememberMeServices.loginSuccess(request, response, authResult);
if (this.eventPublisher != null) {
this.eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
}
this.successHandler.onAuthenticationSuccess(request, response, authResult);
}

위 코드를 보시면 인증에 성공한 이후에 

SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authResult);
SecurityContextHolder.setContext(context);

를 통해 이전의 인증객체를 삭제하고 현재 인증된 정보를 저장하고 있습니다.

스프링 시큐리티의 정확한 흐름만 이해하신다면 크게 걱정하지 않으셔도 됩니다.

감사합니다. 덕분에 많이 배웠습니다 :)