Introduce LogoutSuccessEvent

LogoutSuccessEvent is a simple AbstractAuthenticationEvent implementation which indicates successful logout.

By default, LogoutConfigurer will add a new LogoutHandler called LogoutSuccessEventPublishingLogoutHandler to publish this event.

This PR will also fix ConcurrentSessionFilter's composite logoutHandler, now will get LogoutHandler instances from LogoutConfigurer for consistency.

Fixes gh-2900
This commit is contained in:
Onur Kagan Ozcan
2019-08-23 19:01:24 +03:00
committed by Rob Winch
parent 7576dc44d7
commit 034b5e9e93
10 changed files with 291 additions and 13 deletions

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.authentication.logout;
import org.junit.Test;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.event.LogoutSuccessEvent;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* @author Onur Kagan Ozcan
*/
public class LogoutSuccessEventPublishingLogoutHandlerTests {
@Test
public void shouldPublishEvent() {
LogoutSuccessEventPublishingLogoutHandler handler = new LogoutSuccessEventPublishingLogoutHandler();
LogoutAwareEventPublisher eventPublisher = new LogoutAwareEventPublisher();
handler.setApplicationEventPublisher(eventPublisher);
handler.logout(new MockHttpServletRequest(), new MockHttpServletResponse(), mock(Authentication.class));
assertThat(eventPublisher.flag).isTrue();
}
@Test
public void shouldNotPublishEventWhenAuthenticationIsNull() {
LogoutSuccessEventPublishingLogoutHandler handler = new LogoutSuccessEventPublishingLogoutHandler();
LogoutAwareEventPublisher eventPublisher = new LogoutAwareEventPublisher();
handler.setApplicationEventPublisher(eventPublisher);
handler.logout(new MockHttpServletRequest(), new MockHttpServletResponse(), null);
assertThat(eventPublisher.flag).isFalse();
}
private static class LogoutAwareEventPublisher implements ApplicationEventPublisher {
Boolean flag = false;
@Override
public void publishEvent(Object event) {
if (LogoutSuccessEvent.class.isAssignableFrom(event.getClass())) {
flag = true;
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.security.web.concurrent;
import java.util.Date;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
@@ -53,6 +54,7 @@ import static org.mockito.Mockito.when;
*
* @author Ben Alex
* @author Luke Taylor
* @author Onur Kagan Ozcan
*/
public class ConcurrentSessionFilterTests {
@@ -315,7 +317,7 @@ public class ConcurrentSessionFilterTests {
public void setLogoutHandlersWhenNullThenThrowsException() {
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(new SessionRegistryImpl());
filter.setLogoutHandlers(null);
filter.setLogoutHandlers((List<LogoutHandler>) null);
}
@Test(expected = IllegalArgumentException.class)