|
|
|
@ -15,22 +15,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.security.web.authentication.rememberme; |
|
|
|
package org.springframework.security.web.authentication.rememberme; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.mockito.Matchers.any; |
|
|
|
import static org.mockito.Matchers.any; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import org.junit.*; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.Filter; |
|
|
|
|
|
|
|
import javax.servlet.FilterChain; |
|
|
|
|
|
|
|
import javax.servlet.FilterConfig; |
|
|
|
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
|
|
|
|
import javax.servlet.ServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.ServletResponse; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import junit.framework.TestCase; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.mock.web.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.MockHttpServletResponse; |
|
|
|
import org.springframework.mock.web.MockHttpServletResponse; |
|
|
|
@ -42,6 +31,11 @@ import org.springframework.security.core.AuthenticationException; |
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
|
|
|
import org.springframework.security.web.authentication.NullRememberMeServices; |
|
|
|
import org.springframework.security.web.authentication.NullRememberMeServices; |
|
|
|
import org.springframework.security.web.authentication.RememberMeServices; |
|
|
|
import org.springframework.security.web.authentication.RememberMeServices; |
|
|
|
|
|
|
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.FilterChain; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -49,27 +43,23 @@ import org.springframework.security.web.authentication.RememberMeServices; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Ben Alex |
|
|
|
* @author Ben Alex |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
public class RememberMeAuthenticationFilterTests { |
|
|
|
Authentication remembered = new TestingAuthenticationToken("remembered", "password","ROLE_REMEMBERED"); |
|
|
|
Authentication remembered = new TestingAuthenticationToken("remembered", "password","ROLE_REMEMBERED"); |
|
|
|
|
|
|
|
|
|
|
|
//~ Methods ========================================================================================================
|
|
|
|
//~ Methods ========================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
private void executeFilterInContainerSimulator(FilterConfig filterConfig, Filter filter, ServletRequest request, |
|
|
|
@Before |
|
|
|
ServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
|
|
|
public void setUp() { |
|
|
|
// filter.init(filterConfig);
|
|
|
|
|
|
|
|
filter.doFilter(request, response, filterChain); |
|
|
|
|
|
|
|
// filter.destroy();
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void setUp() throws Exception { |
|
|
|
|
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void tearDown() throws Exception { |
|
|
|
@After |
|
|
|
|
|
|
|
public void tearDown() { |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void testDetectsAuthenticationManagerProperty() throws Exception { |
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
|
|
|
public void testDetectsAuthenticationManagerProperty() { |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
filter.setAuthenticationManager(mock(AuthenticationManager.class)); |
|
|
|
filter.setAuthenticationManager(mock(AuthenticationManager.class)); |
|
|
|
filter.setRememberMeServices(new NullRememberMeServices()); |
|
|
|
filter.setRememberMeServices(new NullRememberMeServices()); |
|
|
|
@ -78,15 +68,11 @@ public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
|
|
|
|
|
|
|
|
filter.setAuthenticationManager(null); |
|
|
|
filter.setAuthenticationManager(null); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
|
|
|
|
fail("Should have thrown IllegalArgumentException"); |
|
|
|
|
|
|
|
} catch (IllegalArgumentException expected) { |
|
|
|
|
|
|
|
assertTrue(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void testDetectsRememberMeServicesProperty() throws Exception { |
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
|
|
|
public void testDetectsRememberMeServicesProperty() { |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
filter.setAuthenticationManager(mock(AuthenticationManager.class)); |
|
|
|
filter.setAuthenticationManager(mock(AuthenticationManager.class)); |
|
|
|
|
|
|
|
|
|
|
|
@ -100,14 +86,10 @@ public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
// check detects if made null
|
|
|
|
// check detects if made null
|
|
|
|
filter.setRememberMeServices(null); |
|
|
|
filter.setRememberMeServices(null); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
|
|
|
|
fail("Should have thrown IllegalArgumentException"); |
|
|
|
|
|
|
|
} catch (IllegalArgumentException expected) { |
|
|
|
|
|
|
|
assertTrue(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testOperationWhenAuthenticationExistsInContextHolder() throws Exception { |
|
|
|
public void testOperationWhenAuthenticationExistsInContextHolder() throws Exception { |
|
|
|
// Put an Authentication object into the SecurityContextHolder
|
|
|
|
// Put an Authentication object into the SecurityContextHolder
|
|
|
|
Authentication originalAuth = new TestingAuthenticationToken("user", "password","ROLE_A"); |
|
|
|
Authentication originalAuth = new TestingAuthenticationToken("user", "password","ROLE_A"); |
|
|
|
@ -121,14 +103,16 @@ public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
|
|
|
|
|
|
|
|
// Test
|
|
|
|
// Test
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
FilterChain fc = mock(FilterChain.class); |
|
|
|
request.setRequestURI("x"); |
|
|
|
request.setRequestURI("x"); |
|
|
|
executeFilterInContainerSimulator(mock(FilterConfig.class), filter, request, new MockHttpServletResponse(), |
|
|
|
filter.doFilter(request, new MockHttpServletResponse(), fc); |
|
|
|
new MockFilterChain(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure filter didn't change our original object
|
|
|
|
// Ensure filter didn't change our original object
|
|
|
|
assertEquals(originalAuth, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
assertSame(originalAuth, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
|
|
|
|
verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testOperationWhenNoAuthenticationInContextHolder() throws Exception { |
|
|
|
public void testOperationWhenNoAuthenticationInContextHolder() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
@ -139,15 +123,17 @@ public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
FilterChain fc = mock(FilterChain.class); |
|
|
|
request.setRequestURI("x"); |
|
|
|
request.setRequestURI("x"); |
|
|
|
executeFilterInContainerSimulator(mock(FilterConfig.class), filter, request, new MockHttpServletResponse(), |
|
|
|
filter.doFilter(request, new MockHttpServletResponse(), fc); |
|
|
|
new MockFilterChain(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure filter setup with our remembered authentication object
|
|
|
|
// Ensure filter setup with our remembered authentication object
|
|
|
|
assertEquals(remembered, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
assertSame(remembered, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
|
|
|
|
verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void testOnUnsuccessfulLoginIsCalledWhenProviderRejectsAuth() throws Exception { |
|
|
|
@Test |
|
|
|
|
|
|
|
public void onUnsuccessfulLoginIsCalledWhenProviderRejectsAuth() throws Exception { |
|
|
|
final Authentication failedAuth = new TestingAuthenticationToken("failed", ""); |
|
|
|
final Authentication failedAuth = new TestingAuthenticationToken("failed", ""); |
|
|
|
|
|
|
|
|
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter() { |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter() { |
|
|
|
@ -164,32 +150,36 @@ public class RememberMeAuthenticationFilterTests extends TestCase { |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
filter.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
FilterChain fc = mock(FilterChain.class); |
|
|
|
request.setRequestURI("x"); |
|
|
|
request.setRequestURI("x"); |
|
|
|
executeFilterInContainerSimulator(mock(FilterConfig.class), filter, request, new MockHttpServletResponse(), |
|
|
|
filter.doFilter(request, new MockHttpServletResponse(), fc); |
|
|
|
new MockFilterChain(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(failedAuth, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
assertSame(failedAuth, SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
|
|
|
|
verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//~ Inner Classes ==================================================================================================
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void authenticationSuccessHandlerIsInvokedOnSuccessfulAuthenticationIfSet() throws Exception { |
|
|
|
private class MockFilterChain implements FilterChain { |
|
|
|
RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(); |
|
|
|
private boolean expectToProceed; |
|
|
|
AuthenticationManager am = mock(AuthenticationManager.class); |
|
|
|
|
|
|
|
when(am.authenticate(remembered)).thenReturn(remembered); |
|
|
|
|
|
|
|
filter.setAuthenticationManager(am); |
|
|
|
|
|
|
|
filter.setRememberMeServices(new MockRememberMeServices(remembered)); |
|
|
|
|
|
|
|
filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/target")); |
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
FilterChain fc = mock(FilterChain.class); |
|
|
|
|
|
|
|
request.setRequestURI("x"); |
|
|
|
|
|
|
|
filter.doFilter(request, response, fc); |
|
|
|
|
|
|
|
|
|
|
|
public MockFilterChain(boolean expectToProceed) { |
|
|
|
assertEquals("/target", response.getRedirectedUrl()); |
|
|
|
this.expectToProceed = expectToProceed; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void doFilter(ServletRequest request, ServletResponse response) |
|
|
|
// Should return after success handler is invoked, so chain should not proceed
|
|
|
|
throws IOException, ServletException { |
|
|
|
verifyZeroInteractions(fc); |
|
|
|
if (expectToProceed) { |
|
|
|
|
|
|
|
assertTrue(true); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
fail("Did not expect filter chain to proceed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//~ Inner Classes ==================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
private class MockRememberMeServices implements RememberMeServices { |
|
|
|
private class MockRememberMeServices implements RememberMeServices { |
|
|
|
private Authentication authToReturn; |
|
|
|
private Authentication authToReturn; |
|
|
|
|
|
|
|
|
|
|
|
|