2 Eylül 2021 Perşembe

SpringSecurity AuthenticationSuccessEvent ve AbstractAuthenticationFailureEvent Sınıfları

Giriş
Şu satırı dahil ederiz. Bu event'ler SpringSecurity tarafından otomatik yayınlanır.
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
Açıklaması şöyle
Spring Security provides both Authentication Events and Authorization Events, although the latter is not typically used and is not published by default if you need to use them then you would need to inform to the Spring Security to publish them by creating an event publisher. However, the Authentication Success Event is published whenever a user is successfully authenticated and added to the Security Context.
Kalıtım şöyle
AbstractAuthenticationEvent
  AbstractAuthenticationFailureEvent
  AuthenticationSuccessEvent
  AuthenticationSwitchUserEvent 
  InteractiveAuthenticationSuccessEvent
  SessionFixationProtectionEvent

Bu yazıda Authentication Events gösteriliyor. Bunlar iki tane
1. AuthenticationSuccessEvent 
2. AbstractAuthenticationFailureEvent 

getAuthentication metodu
Authentication nesnesi döner.

Örnek
Şöyle yaparız
@EventListener
public void loginEventListener(AuthenticationSuccessEvent authenticationSuccessEvent){
  UserDetails principal = (UserDetails) authenticationSuccessEvent.getAuthentication()
    .getPrincipal();
  ...
}
@EventListener
public void onFailure(AbstractAuthenticationFailureEvent failures) {
  System.out.println(failures);
  ...
}
Örnek
Şöyle yaparız
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;

@EventListener
public void authenticationSuccess(AuthenticationSuccessEvent event) {

  String name = event.getAuthentication().getName()
}

@EventListener
public void authenticationFailed(AbstractAuthenticationFailureEvent event) {

   String name = event.getAuthentication().getName();
}

Hiç yorum yok:

Yorum Gönder