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

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

Introduction

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

Prototype

public AuthenticationSuccessEvent(Authentication authentication) 

Source Link

Usage

From source file:org.jasig.ssp.service.impl.ScheduledTaskWrapperServiceImpl.java

/**
 * Decorates the given {@code Runnable} with a login and logout of
 * {@link org.jasig.ssp.service.SecurityService#noAuthAdminUser()}.
 *
 * <p>Prior to <a href="https://issues.jasig.org/browse/SSP-2241">SSP-2241</a>
 * we didn't attempt to ensure any particular {@link SecurityContext} state
 * prior to running jobs. This ended up causing a memory leak because our
 * Hibernate flush interceptor would generate a new {@link SspUser} for
 * every flushed "auditer" field, and every time that happened, that
 * {@link SspUser} was added to a {@code ThreadLocal} list. For a large
 * job like {@link #syncExternalPersons()}, the growth of that list was
 * particularly explosive. {@link SspUser} is definitely due for a refactor
 * to eliminate it's {@code ThreadLocal} dependencies, but for the time
 * being we're able to short-circuit the leak by ensuring that there is
 * a current {@link Authentication} that the Hibernate flush interceptor
 * will honor. (It will not honor the anonymous user.) And this is good
 * practice anyway - to always explicitly set up a security context rather
 * than let obscure Hibernate extension internals make up the rules as we
 * go.</p>// www. ja  v  a  2 s .  c  o m
 *
 * @see #withMaybeSudo(Runnable)
 * @param work
 * @return
 * @throws AuthenticationException
 */
protected Runnable withSudo(final Runnable work, final UUID runAsId) throws AuthenticationException {
    return new Runnable() {
        @Override
        public void run() {
            final SspUser runAs;
            if (runAsId == null) {
                runAs = securityService.noAuthAdminUser();
            } else {
                try {
                    final Person person = personService.get(runAsId);
                    if (person == null) {
                        throw new ObjectNotFoundException(runAsId, Person.class.getName());
                    }

                    // mostly copy/paste from UPortalSecurityFilter
                    final Set<Assignment> assignments = PermissionsService.IMPL.get()
                            .getAssignmentsForPerson(person.getUsername(), true);

                    // Find SSP-related permissions in the assignments collection
                    final Set<GrantedAuthority> authorities = Sets.newHashSet();
                    for (Assignment a : assignments) {
                        if (a.getOwner().getKey().equals(UPortalSecurityFilter.SSP_OWNER)) {
                            // This one pertains to us...
                            String activity = a.getActivity().getKey();
                            authorities.add(new GrantedAuthorityImpl("ROLE_" + activity));
                        }
                    }

                    final SspUser user = new SspUser(person.getUsername(), "", true, true, true, true,
                            authorities);

                    user.setPerson(person);
                    runAs = user;
                } catch (ObjectNotFoundException e) {
                    throw new UsernameNotFoundException("Could not find Person by ID [" + runAsId + "]", e);
                }

            }
            Authentication auth = new RunAsUserToken(runAsKey, runAs, null, runAs.getAuthorities(), null);
            auth = authenticationManager.authenticate(auth);

            // Not sure why/if we need this. Just trying to mimic long-time
            // legacy behavior in UPortalPreAuthenticatedProcessingFilter
            if (eventPublisher != null) {
                eventPublisher.publishEvent(new AuthenticationSuccessEvent(auth));
            }

            // AuthenticationManager doesn't do this for you
            SecurityContextHolder.getContext().setAuthentication(auth);

            try {
                work.run();
            } finally {
                SecurityContextHolder.getContext().setAuthentication(null);
            }
        }
    };
}

From source file:org.springframework.security.authentication.DefaultAuthenticationEventPublisher.java

public void publishAuthenticationSuccess(Authentication authentication) {
    if (applicationEventPublisher != null) {
        applicationEventPublisher.publishEvent(new AuthenticationSuccessEvent(authentication));
    }/*from  w w w .j ava 2s.  c om*/
}