61. Authentication success listener (#6)

This commit is contained in:
Art
2021-09-08 14:11:04 +03:00
parent d272b01ff9
commit 6945bcef59

View File

@ -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<AuthenticationSuccessEvent> {
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);
}
}
}