Refactored Brute Force Detection Listeners (#6)
This commit is contained in:
@ -1,23 +0,0 @@
|
|||||||
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<AuthenticationFailureBadCredentialsEvent> {
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -3,7 +3,8 @@ package net.shyshkin.study.fullstack.supportportal.backend.listener;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.service.LoginAttemptService;
|
import net.shyshkin.study.fullstack.supportportal.backend.service.LoginAttemptService;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
|
||||||
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -11,12 +12,19 @@ import org.springframework.stereotype.Component;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
|
public class BruteForceDetectionListeners {
|
||||||
|
|
||||||
private final LoginAttemptService loginAttemptService;
|
private final LoginAttemptService loginAttemptService;
|
||||||
|
|
||||||
@Override
|
@EventListener
|
||||||
public void onApplicationEvent(AuthenticationSuccessEvent event) {
|
public void onLoginFailure(AuthenticationFailureBadCredentialsEvent event) {
|
||||||
|
String username = event.getAuthentication().getPrincipal().toString();
|
||||||
|
log.debug("{} failed to login", username);
|
||||||
|
loginAttemptService.loginFailed(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
public void onLoginSuccess(AuthenticationSuccessEvent event) {
|
||||||
Object principal = event.getAuthentication().getPrincipal();
|
Object principal = event.getAuthentication().getPrincipal();
|
||||||
if (principal instanceof UserDetails) {
|
if (principal instanceof UserDetails) {
|
||||||
String username = ((UserDetails) principal).getUsername();
|
String username = ((UserDetails) principal).getUsername();
|
||||||
Reference in New Issue
Block a user