diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationFailureListener.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationFailureListener.java new file mode 100644 index 0000000..beef41e --- /dev/null +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationFailureListener.java @@ -0,0 +1,23 @@ +package net.shyshkin.study.fullstack.supportportal.backend.listener; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import net.shyshkin.study.fullstack.supportportal.backend.service.LoginAttemptService; +import org.springframework.context.ApplicationListener; +import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class AuthenticationFailureListener implements ApplicationListener { + + private final LoginAttemptService loginAttemptService; + + @Override + public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) { + String username = event.getAuthentication().getPrincipal().toString(); + log.debug("{} failed to login", username); + loginAttemptService.loginFailed(username); + } +}