diff --git a/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java b/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java new file mode 100644 index 0000000000..846e2b4a7e --- /dev/null +++ b/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java @@ -0,0 +1,81 @@ +/* + * Copyright 2010 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 + * + * http://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; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.security.web.util.RequestMatcher; +import org.springframework.util.Assert; + +/** + * An AuthenticationEntryPoint which selects a concrete EntryPoint based on a + * RequestMatcher evaluation. + * + * @author Mike Wiesner + * @since 3.0.2 + * @version $Id:$ + */ +public class DelegatingAuthenticationEntryPoint implements + AuthenticationEntryPoint, InitializingBean { + + private LinkedHashMap entryPoints; + private AuthenticationEntryPoint defaultEntryPoint; + + public DelegatingAuthenticationEntryPoint( + LinkedHashMap entryPoints) { + this.entryPoints = entryPoints; + } + + /** + * EntryPoint which is used when no RequestMatcher returned true + */ + public void setDefaultEntryPoint(AuthenticationEntryPoint defaultEntryPoint) { + this.defaultEntryPoint = defaultEntryPoint; + } + + + public void commence(HttpServletRequest request, + HttpServletResponse response, AuthenticationException authException) + throws IOException, ServletException { + + for (RequestMatcher requestMatcher : entryPoints.keySet()) { + if (requestMatcher.matches(request)) + { + entryPoints.get(requestMatcher).commence(request, response, authException); + return; + } + } + + // No EntryPoint matched, use defaultEntryPoint + defaultEntryPoint.commence(request, response, authException); + } + + public void afterPropertiesSet() throws Exception { + Assert.notEmpty(entryPoints, "entryPoints must be specified"); + Assert.notNull(defaultEntryPoint, "defaultEntryPoint must be specified"); + } + + +} diff --git a/web/src/test/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPointTest.java b/web/src/test/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPointTest.java new file mode 100644 index 0000000000..86d3a6fda5 --- /dev/null +++ b/web/src/test/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPointTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2010 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 + * + * http://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; + +import static org.mockito.Mockito.*; + +import java.util.LinkedHashMap; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.security.web.util.RequestMatcher; + +/** + * Test class for {@link DelegatingAuthenticationEntryPoint} + * + * @author Mike Wiesner + * @since 3.0.2 + * @version $Id:$ + */ +public class DelegatingAuthenticationEntryPointTest { + + private DelegatingAuthenticationEntryPoint daep; + private LinkedHashMap entryPoints; + private AuthenticationEntryPoint defaultEntryPoint; + private HttpServletRequest request = new MockHttpServletRequest(); + + @Before + public void before() { + defaultEntryPoint = mock(AuthenticationEntryPoint.class); + entryPoints = new LinkedHashMap(); + daep = new DelegatingAuthenticationEntryPoint(entryPoints); + daep.setDefaultEntryPoint(defaultEntryPoint); + } + + @Test + public void testDefaultEntryPoint() throws Exception { + AuthenticationEntryPoint firstAEP = mock(AuthenticationEntryPoint.class); + RequestMatcher firstRM = mock(RequestMatcher.class); + when(firstRM.matches(request)).thenReturn(false); + entryPoints.put(firstRM, firstAEP); + + daep.commence(request, null, null); + + verify(defaultEntryPoint).commence(request, null, null); + verify(firstAEP, never()).commence(request, null, null); + } + + @Test + public void testFirstEntryPoint() throws Exception { + AuthenticationEntryPoint firstAEP = mock(AuthenticationEntryPoint.class); + RequestMatcher firstRM = mock(RequestMatcher.class); + AuthenticationEntryPoint secondAEP = mock(AuthenticationEntryPoint.class); + RequestMatcher secondRM = mock(RequestMatcher.class); + when(firstRM.matches(request)).thenReturn(true); + entryPoints.put(firstRM, firstAEP); + entryPoints.put(secondRM, secondAEP); + + daep.commence(request, null, null); + + verify(firstAEP).commence(request, null, null); + verify(secondAEP, never()).commence(request, null, null); + verify(defaultEntryPoint, never()).commence(request, null, null); + verify(secondRM, never()).matches(request); + } + + @Test + public void testSecondEntryPoint() throws Exception { + AuthenticationEntryPoint firstAEP = mock(AuthenticationEntryPoint.class); + RequestMatcher firstRM = mock(RequestMatcher.class); + AuthenticationEntryPoint secondAEP = mock(AuthenticationEntryPoint.class); + RequestMatcher secondRM = mock(RequestMatcher.class); + when(firstRM.matches(request)).thenReturn(false); + when(secondRM.matches(request)).thenReturn(true); + entryPoints.put(firstRM, firstAEP); + entryPoints.put(secondRM, secondAEP); + + daep.commence(request, null, null); + + verify(secondAEP).commence(request, null, null); + verify(firstAEP, never()).commence(request, null, null); + verify(defaultEntryPoint, never()).commence(request, null, null); + } + +}