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

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

Introduction

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

Prototype

public Authentication getAuthentication() 

Source Link

Document

Getters for the Authentication request that caused the event.

Usage

From source file:fr.univrouen.poste.provider.AuthenticationSuccessEventListener.java

@Override
@Transactional/*from w  ww  .j  ava 2  s .c om*/
public void onApplicationEvent(AuthenticationSuccessEvent ev) {

    String username = ev.getAuthentication().getName();

    TypedQuery<User> query = User.findUsersByEmailAddress(username, null, null);
    User targetUser = (User) query.getSingleResult();

    if (targetUser != null) { // only for existing users
        targetUser.reportLoginOK();
        targetUser.persist();
    }
}

From source file:com.example.securelogin.domain.service.account.AccountAuthenticationSuccessEventListener.java

@EventListener
public void onApplicationEvent(AuthenticationSuccessEvent event) {
    LoggedInUser details = (LoggedInUser) event.getAuthentication().getPrincipal();

    authenticationEventSharedService.authenticationSuccess(details.getUsername());
}

From source file:io.gravitee.management.security.listener.AuthenticationSuccessListener.java

@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
    final Authentication authentication = event.getAuthentication();
    try {//  w w w .  j  ava2s  . co  m
        userService.findByName(authentication.getName());
    } catch (UserNotFoundException unfe) {
        final NewUserEntity newUser = new NewUserEntity();
        newUser.setUsername(authentication.getName());
        final Set<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority)
                .collect(Collectors.toSet());
        newUser.setRoles(roles);
        userService.create(newUser);
    }
}

From source file:com.kazuki43zoo.jpetstore.ui.EventListeners.java

@EventListener
public void handleAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
    Account account = ((AccountUserDetails) event.getAuthentication().getPrincipal()).getAccount();
    loadFavouriteProductList(account);/* w w w . j a v  a 2s  .  c o  m*/
}

From source file:cn.org.once.cstack.config.UserAuthenticationSuccess.java

@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
    try {/*from   w  w  w  .  jav a  2 s  . c  o  m*/
        User user = userService
                .findByLogin(((UserDetails) event.getAuthentication().getPrincipal()).getUsername());
        user.setLastConnection(new Date());
        userService.update(user);
    } catch (ServiceException e) {
        e.printStackTrace();
    }
}

From source file:eu.supersede.fe.security.AuthenticationSuccessListener.java

@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    DatabaseUser userDetails = (DatabaseUser) event.getAuthentication().getPrincipal();

    HttpServletRequest req = attr.getRequest();
    String multiTenantId = req.getHeader("TenantId");
    userDetails.setTenantId(multiTenantId);
}

From source file:com.devnexus.ting.core.applicationlistener.SecurityEventListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {

    if (event instanceof AuthenticationSuccessEvent) {

        final AuthenticationSuccessEvent successEvent = (AuthenticationSuccessEvent) event;
        LOGGER.info("Successful Authentication of User: " + successEvent.getAuthentication().getName());
        successEvent.getAuthentication();

        final User user = (User) successEvent.getAuthentication().getPrincipal();
        userService.trackUserLogin(user);

    } else if (event instanceof InteractiveAuthenticationSuccessEvent) {

        final InteractiveAuthenticationSuccessEvent successEvent = (InteractiveAuthenticationSuccessEvent) event;
        LOGGER.info(// ww  w . j av  a  2 s.c  o m
                "Successful Interactive Authentication of User: " + successEvent.getAuthentication().getName());

    } else if (event instanceof AbstractAuthenticationFailureEvent) {

        final AbstractAuthenticationFailureEvent failureEvent = (AbstractAuthenticationFailureEvent) event;
        LOGGER.warn("Authentication Failure for User '" + failureEvent.getAuthentication().getPrincipal() + "' "
                + failureEvent.getException().getMessage(), failureEvent.getException());

    } else {

        /*
         * Since we really don't care about other events, we log it for
         *  now, but because of that we don't throw an explicit exception.
         */
        LOGGER.error("Unhandled AuthenticationEvent (" + event.getClass().getName() + ") for user '"
                + event.getAuthentication().getPrincipal() + "':" + event.toString());
    }
}

From source file:org.opentides.service.impl.UserServiceImpl.java

/**
 * Updates last login of the user from a login event. Also logs the event in
 * history log.//w  w w .  j a v a  2 s . com
 */
public void updateLogin(AuthenticationSuccessEvent authenticationSuccessEvent) {
    UserDao userDao = (UserDao) getDao();
    String username = authenticationSuccessEvent.getAuthentication().getName();

    if (username != null && !username.isEmpty()) {
        BaseUser user = userDao.loadByUsername(username);
        WebAuthenticationDetails details = (WebAuthenticationDetails) authenticationSuccessEvent
                .getAuthentication().getDetails();
        if (details != null) {
            String address = details.getRemoteAddress();
            if (user.getTotalLoginCount() == null)
                user.setTotalLoginCount(1l);
            else
                user.setTotalLoginCount(user.getTotalLoginCount() + 1);
            user.setPrevLoginIP(user.getLastLoginIP());
            user.setLastLoginIP(address);
            user.setLastLogin(new Date());
            user.setSkipAudit(true);
            userDao.saveEntityModel(user);

            // force the audit user details
            String completeName = user.getCompleteName() + " [" + username + "] ";
            user.setAuditUserId(user.getId());
            user.setAuditUsername(username);
            user.setSkipAudit(false);
            String message = completeName + " has logged-in. IP Address: " + user.getLastLoginIP();
            AuditLogDaoImpl.logEvent(message, user);
        }
    }
}

From source file:org.opennms.web.springframework.security.SecurityAuthenticationEventOnmsEventBuilder.java

/** {@inheritDoc} */
@Override/*ww  w.  j  av a  2 s.  co m*/
public void onApplicationEvent(ApplicationEvent event) {
    LOG.debug("Received ApplicationEvent {}", event.getClass());
    if (event instanceof AuthenticationSuccessEvent) {
        AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;

        EventBuilder builder = createEvent(SUCCESS_UEI, authEvent);
        // Sync the timestamp
        builder.setTime(new Date(event.getTimestamp()));
        if (!"true".equalsIgnoreCase(System.getProperty("org.opennms.security.disableLoginSuccessEvent"))) {
            sendEvent(builder.getEvent());
        }
    }

    if (event instanceof AbstractAuthenticationFailureEvent) {
        AbstractAuthenticationFailureEvent authEvent = (AbstractAuthenticationFailureEvent) event;

        LOG.debug("AbstractAuthenticationFailureEvent was received, exception message - {}",
                authEvent.getException().getMessage());
        EventBuilder builder = createEvent(FAILURE_UEI, authEvent);
        // Sync the timestamp
        builder.setTime(new Date(event.getTimestamp()));
        builder.addParam("exceptionName", authEvent.getException().getClass().getSimpleName());
        builder.addParam("exceptionMessage", authEvent.getException().getMessage());
        sendEvent(builder.getEvent());
    }

    if (event instanceof AuthorizedEvent) {
        AuthorizedEvent authEvent = (AuthorizedEvent) event;
        LOG.debug("AuthorizedEvent received - \n  Details - {}\n  Principal - {}",
                authEvent.getAuthentication().getDetails(), authEvent.getAuthentication().getPrincipal());
    }
    if (event instanceof AuthorizationFailureEvent) {
        AuthorizationFailureEvent authEvent = (AuthorizationFailureEvent) event;
        LOG.debug("AuthorizationFailureEvent received  -\n   Details - {}\n  Principal - {}",
                authEvent.getAuthentication().getDetails(), authEvent.getAuthentication().getPrincipal());
    }
    if (event instanceof InteractiveAuthenticationSuccessEvent) {
        InteractiveAuthenticationSuccessEvent authEvent = (InteractiveAuthenticationSuccessEvent) event;
        LOG.debug("InteractiveAuthenticationSuccessEvent received - \n  Details - {}\n  Principal - {}",
                authEvent.getAuthentication().getDetails(), authEvent.getAuthentication().getPrincipal());

    }
    if (event instanceof ServletRequestHandledEvent) {
        ServletRequestHandledEvent authEvent = (ServletRequestHandledEvent) event;
        LOG.debug("ServletRequestHandledEvent received - {}\n  Servlet - {}\n  URL - {}",
                authEvent.getDescription(), authEvent.getServletName(), authEvent.getRequestUrl());
        LOG.info("{} requested from {} by user {}", authEvent.getRequestUrl(), authEvent.getClientAddress(),
                authEvent.getUserName());
    }

}