• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

AbstratSecurityInterceptor에서의 Event의 용도가 궁금합니다

21.12.21 17:55 작성 조회수 151

1

// AbstractSecurityInterceptor.class

	private void attemptAuthorization(Object object, Collection<ConfigAttribute> attributes,
			Authentication authenticated) {
		try {
			this.accessDecisionManager.decide(authenticated, object, attributes);
		}
		catch (AccessDeniedException ex) {
			if (this.logger.isTraceEnabled()) {
				this.logger.trace(LogMessage.format("Failed to authorize %s with attributes %s using %s", object,
						attributes, this.accessDecisionManager));
			}
			else if (this.logger.isDebugEnabled()) {
				this.logger.debug(LogMessage.format("Failed to authorize %s with attributes %s", object, attributes));
			}
			publishEvent(new AuthorizationFailureEvent(object, attributes, authenticated, ex));
			throw ex;
		}
	}


여기서 publishEvent를 진행하는데요, 제가 이해한 바로는 ExceptionTranslationFilter에서 try-catch로 작업을 진행하기 때문에, 해당 로직(예외처리)에는 불필요해보이는 로직이라 생각합니다.

이 부분의 역할에 대해 궁금증이 생겨 질문 남깁니다!

코드를 분석해봤을 때에는, LoggerListener에서 이 이벤트를 로그로 작성해주고 있는데, 해당 부분만을 별도의 로그 관리로 진행하는지?에 대해서도 의문이남네요!


답변 1

답변을 작성해보세요.

0

먼저 답변이 늦어 죄송합니다.

말씀하신 것처럼 LoggerListener 에서 로그를 남기는 작업을 하고 있습니다.

public class LoggerListener implements ApplicationListener<AbstractAuthorizationEvent> {

private static final Log logger = LogFactory.getLog(LoggerListener.class);

@Override
public void onApplicationEvent(AbstractAuthorizationEvent event) {
if (event instanceof AuthenticationCredentialsNotFoundEvent) {
onAuthenticationCredentialsNotFoundEvent((AuthenticationCredentialsNotFoundEvent) event);
}
if (event instanceof AuthorizationFailureEvent) {
onAuthorizationFailureEvent((AuthorizationFailureEvent) event);
}
if (event instanceof AuthorizedEvent) {
onAuthorizedEvent((AuthorizedEvent) event);
}
if (event instanceof PublicInvocationEvent) {
onPublicInvocationEvent((PublicInvocationEvent) event);
}
}

즉 이벤트의 유형에 따라 로그를 남기는 작업을 하고 있습니다.

private void onAuthorizationFailureEvent(AuthorizationFailureEvent authEvent) {
logger.warn(LogMessage.format(
"Security authorization failed due to: %s; authenticated principal: %s; secure object: %s; configuration attributes: %s",
authEvent.getAccessDeniedException(), authEvent.getAuthentication(), authEvent.getSource(),
authEvent.getConfigAttributes()));
}

어떻게 보면 로그를 남기는 작업은 비즈니스 로직의 핵심기능은 아닙니다.

그래서 흔히  AOP 를 활용해서 로깅작업만 별도로 하기도 합니다.

AbstractSecurityInterceptor 클래스는 권한 관련 책임만 담당하고 로깅작업은 별도의 이벤트를 통해 처리한다고 보시면 될 것 같습니다.

비단 여기뿐 아니라 시규리티의 다른 곳에서도 이벤트를 활용해서 여러 작업들을 하고 있습니다.

 

 

김태완님의 프로필

김태완

질문자

2022.01.05

친절한 설명 감사합니다 :)