Example usage for org.springframework.security.authentication.event InteractiveAuthenticationSuccessEvent getAuthentication

List of usage examples for org.springframework.security.authentication.event InteractiveAuthenticationSuccessEvent getAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.authentication.event InteractiveAuthenticationSuccessEvent getAuthentication.

Prototype

public Authentication getAuthentication() 

Source Link

Document

Getters for the Authentication request that caused the event.

Usage

From source file:de.pksoftware.springstrap.core.service.AccountServiceBase.java

/**
 * Triggered on account login./* w  ww .  j a  va 2 s .c  o m*/
 * @param event The Spring interactive authentication event.
 * TODO implement lastLoginIp and lastVisitIp
 */
@Override
@Transactional
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
    Authentication userAuth = event.getAuthentication();

    logger.info("Authenticated using {}", userAuth);

    String remoteAddress = null;

    WebAuthenticationDetails details = (WebAuthenticationDetails) userAuth.getDetails();
    if (null != details) {
        remoteAddress = details.getRemoteAddress();
    } else {
        logger.warn("Cannot determine remote address!");
    }

    IAccount account = (IAccount) userAuth.getPrincipal();

    ZonedDateTime now = ZonedDateTime.now();

    ZonedDateTime lastLoginDate = account.getLastLoginDate();
    String lastLoginIp = account.getLastLoginIp();

    if (null != lastLoginDate) {
        account.setLastVisitDate(lastLoginDate);
    }

    if (null != lastLoginIp) {
        account.setLastVisitIp(lastLoginIp);
    }

    account.setLastLoginDate(now);
    account.setLastLoginIp(remoteAddress);

    this.saveAccount(account);

    logger.info("SUCCESSFUL Authentication with {}", account);
}