From 6945bcef59eeb94427d93d1dc38b176b98283dc4 Mon Sep 17 00:00:00 2001 From: Art Date: Wed, 8 Sep 2021 14:11:04 +0300 Subject: [PATCH] 61. Authentication success listener (#6) --- .../AuthenticationSuccessListener.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationSuccessListener.java diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationSuccessListener.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationSuccessListener.java new file mode 100644 index 0000000..bfe44b3 --- /dev/null +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/listener/AuthenticationSuccessListener.java @@ -0,0 +1,27 @@ +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.AuthenticationSuccessEvent; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class AuthenticationSuccessListener implements ApplicationListener { + + private final LoginAttemptService loginAttemptService; + + @Override + public void onApplicationEvent(AuthenticationSuccessEvent event) { + Object principal = event.getAuthentication().getPrincipal(); + if (principal instanceof UserDetails) { + String username = ((UserDetails) principal).getUsername(); + log.debug("{} successfully logged in", username); + loginAttemptService.loginSucceeded(username); + } + } +}