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

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

Introduction

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

Prototype

public Authentication getAuthentication() 

Source Link

Document

Getters for the Authentication request that caused the event.

Usage

From source file:org.shaigor.rest.retro.client.oauth.OAuthPostAuthListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {
    Authentication authentication = event.getAuthentication();

    if (event instanceof AuthenticationSuccessEvent) {
        ResourceOwnerPasswordResourceDetails resource = getResourceOwnerPasswordResourceDetails();
        resource.setScope(Arrays.asList("words"));
        resource.setUsername(authentication.getName());
        resource.setPassword(authentication.getCredentials().toString());

        try {//w  w  w  .j  a  v a 2  s. c  om
            OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken(resource,
                    new DefaultAccessTokenRequest());
            log.debug("Access token request succeeded for user: '{}', new token is '{}'",
                    resource.getUsername(), accessToken.getValue());
            if (authentication instanceof AbstractAuthenticationToken
                    && authentication.getDetails() instanceof CustomAuthenticationDetails) {
                ((CustomAuthenticationDetails) ((AbstractAuthenticationToken) authentication).getDetails())
                        .setBearer(accessToken.getValue());
                log.debug("Access token was added to authentication as details");
            } else if (log.isDebugEnabled()) {
                log.debug("Access token could not be added to authentication as details");
            }
        } catch (Exception e) {
            log.error("Access token request failed for user: '" + resource.getUsername() + "'", e);
        }
    }
    if (authentication instanceof CredentialsContainer) {
        // Authentication is complete. Remove credentials and other secret data from authentication
        ((CredentialsContainer) authentication).eraseCredentials();
    }

}

From source file:com.thesoftwareguild.flightmaster.debugging.AuthenticationEventListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
    if (authenticationEvent instanceof InteractiveAuthenticationSuccessEvent) {
        // ignores to prevent duplicate logging with AuthenticationSuccessEvent
        return;/*from w w  w .j a  v a 2s  . c o m*/
    }
    Authentication authentication = authenticationEvent.getAuthentication();
    String auditMessage = "Login attempt with username: " + authentication.getName() + "\t\tSuccess: "
            + authentication.isAuthenticated();
    logger.info(auditMessage);
}

From source file:mum.edu.sec.AuthenticationEventListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
    if (authenticationEvent instanceof InteractiveAuthenticationSuccessEvent) {
        // ignores to prevent duplicate logging with AuthenticationSuccessEvent
        return;/*  w  w w  .  j  a va2s  .c  o  m*/
    }
    Authentication authentication = authenticationEvent.getAuthentication();
    String auditMessage = "Login attempt with username: " + authentication.getName() + " pass: '"
            + authentication.getCredentials() + "'" + "\t\tSuccess: " + authentication.isAuthenticated();
    System.err.println(auditMessage);
    //logger.info(auditMessage);
}

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  .jav  a  2 s .co  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.opennms.web.springframework.security.SecurityAuthenticationEventOnmsEventBuilder.java

private EventBuilder createEvent(String uei, AbstractAuthenticationEvent authEvent) {
    EventBuilder builder = new EventBuilder(uei, "OpenNMS.WebUI");
    builder.setTime(new Date(authEvent.getTimestamp()));
    org.springframework.security.core.Authentication auth = authEvent.getAuthentication();
    if (auth != null && auth.getName() != null) {
        builder.addParam("user", WebSecurityUtils.sanitizeString(auth.getName()));
    }/*from   w w w.j  a  v a  2s.  c  o m*/
    if (auth != null && auth.getDetails() != null && auth.getDetails() instanceof WebAuthenticationDetails) {
        WebAuthenticationDetails webDetails = (WebAuthenticationDetails) auth.getDetails();
        if (webDetails.getRemoteAddress() != null) {
            builder.addParam("ip", webDetails.getRemoteAddress());
        }
    }
    return builder;
}

From source file:org.springframework.security.authentication.event.LoggerListener.java

public void onApplicationEvent(AbstractAuthenticationEvent event) {
    if (!logInteractiveAuthenticationSuccessEvents && event instanceof InteractiveAuthenticationSuccessEvent) {
        return;//from w  w w .j av a 2 s  .c o m
    }

    if (logger.isWarnEnabled()) {
        final StringBuilder builder = new StringBuilder();
        builder.append("Authentication event ");
        builder.append(ClassUtils.getShortName(event.getClass()));
        builder.append(": ");
        builder.append(event.getAuthentication().getName());
        builder.append("; details: ");
        builder.append(event.getAuthentication().getDetails());

        if (event instanceof AbstractAuthenticationFailureEvent) {
            builder.append("; exception: ");
            builder.append(((AbstractAuthenticationFailureEvent) event).getException().getMessage());
        }

        logger.warn(builder.toString());
    }
}