下面列出了怎么用org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@EventListener
public void loginFailureListener(AbstractAuthenticationFailureEvent event) {
if (event.getSource() instanceof AbstractAuthenticationToken) {
AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
Object details = token.getDetails();
if (details instanceof WebAuthenticationDetails) {
LOG.info("Login failed from [{}]", ((WebAuthenticationDetails) details).getRemoteAddress());
}
}
}
/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
AuthenticationException authenticationException = event.getException();
Authentication authentication = (Authentication) event.getSource();
handle(authenticationException, authentication);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof AbstractAuthenticationFailureEvent) {
if (event.getSource() instanceof AbstractAuthenticationToken) {
AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
Object details = token.getDetails();
if (details instanceof WebAuthenticationDetails) {
LOG.info("Login failed from [" + ((WebAuthenticationDetails) details).getRemoteAddress() + "]");
}
}
}
}
@EventListener
public void handleAuthenticationFailure( AbstractAuthenticationFailureEvent event )
{
Authentication auth = event.getAuthentication();
if ( TwoFactorWebAuthenticationDetails.class.isAssignableFrom( auth.getDetails().getClass() ) )
{
TwoFactorWebAuthenticationDetails authDetails =
( TwoFactorWebAuthenticationDetails ) auth.getDetails();
log.info( String.format( "Login attempt failed for remote IP: %s", authDetails.getIp() ) );
}
securityService.registerFailedLogin( auth.getName() );
}
public void onApplicationEvent( AbstractAuthenticationEvent event )
{
if ( log.isWarnEnabled() )
{
final StringBuilder builder = new StringBuilder();
builder.append( "Authentication event " );
builder.append( ClassUtils.getShortName( event.getClass() ) );
builder.append( ": " );
builder.append( event.getAuthentication().getName() );
Object details = event.getAuthentication().getDetails();
if ( ForwardedIpAwareWebAuthenticationDetails.class.isAssignableFrom( details.getClass() ) )
{
ForwardedIpAwareWebAuthenticationDetails authDetails = (ForwardedIpAwareWebAuthenticationDetails) details;
String ip = authDetails.getIp();
builder.append( "; ip: " );
builder.append( ip );
String sessionId = authDetails.getSessionId();
if ( sessionId != null )
{
HashCode hash = Hashing.sha256().newHasher().putString( sessionId, Charsets.UTF_8 ).hash();
builder.append( " sessionId: " );
builder.append( hash.toString() );
}
}
if ( event instanceof AbstractAuthenticationFailureEvent )
{
builder.append( "; exception: " );
builder.append( ((AbstractAuthenticationFailureEvent) event).getException().getMessage() );
}
log.warn( builder.toString() );
}
}
@Override
public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
log.info("User Oauth2 login Failed, " + event.getException().getMessage());
}
@Override
public void onApplicationEvent(AbstractAuthenticationFailureEvent e) {
GraviteeAuthenticationDetails auth = (GraviteeAuthenticationDetails) e.getAuthentication().getDetails();
LOGGER.warn("Authentication failed event for: " + e.getAuthentication().getPrincipal() + " - IP: " + auth.getRemoteAddress());
}