Polish HttpConfigurationTests

This commit is contained in:
Joe Grandja 2018-03-05 08:36:15 -05:00
parent 2a678ebc6e
commit 5af1d1d936
1 changed files with 11 additions and 25 deletions

View File

@ -25,19 +25,20 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.test.SpringTestRule; import org.springframework.security.config.test.SpringTestRule;
import org.springframework.security.core.userdetails.PasswordEncodedUser; import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.ThrowableAssert.catchThrowable; import static org.assertj.core.api.ThrowableAssert.catchThrowable;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -51,9 +52,6 @@ public class HttpConfigurationTests {
@Rule @Rule
public final SpringTestRule spring = new SpringTestRule(); public final SpringTestRule spring = new SpringTestRule();
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@ -93,16 +91,22 @@ public class HttpConfigurationTests {
// https://github.com/SpringSource/spring-security-javaconfig/issues/104 // https://github.com/SpringSource/spring-security-javaconfig/issues/104
@Test @Test
public void configureWhenAddFilterCasAuthenticationFilterThenFilterAdded() throws Exception { public void configureWhenAddFilterCasAuthenticationFilterThenFilterAdded() throws Exception {
CasAuthenticationFilterConfig.CAS_AUTHENTICATION_FILTER = spy(new CasAuthenticationFilter());
this.spring.register(CasAuthenticationFilterConfig.class).autowire(); this.spring.register(CasAuthenticationFilterConfig.class).autowire();
assertThat(this.findFilter(CasAuthenticationFilter.class, this.springSecurityFilterChain)).isNotNull(); this.mockMvc.perform(get("/"));
verify(CasAuthenticationFilterConfig.CAS_AUTHENTICATION_FILTER).doFilter(
any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class));
} }
@EnableWebSecurity @EnableWebSecurity
static class CasAuthenticationFilterConfig extends WebSecurityConfigurerAdapter { static class CasAuthenticationFilterConfig extends WebSecurityConfigurerAdapter {
static CasAuthenticationFilter CAS_AUTHENTICATION_FILTER;
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http
.addFilter(new CasAuthenticationFilter()); .addFilter(CAS_AUTHENTICATION_FILTER);
} }
} }
@ -131,22 +135,4 @@ public class HttpConfigurationTests {
.httpBasic(); .httpBasic();
} }
} }
private <T extends Filter> T findFilter(Class<T> filterType, FilterChainProxy filterChainProxy) {
return this.findFilter(filterType, filterChainProxy, 0);
}
private <T extends Filter> T findFilter(Class<T> filterType, FilterChainProxy filterChainProxy, int filterChainIndex) {
if (filterChainIndex >= filterChainProxy.getFilterChains().size()) {
return null;
}
Filter filter = filterChainProxy.getFilterChains().get(filterChainIndex).getFilters()
.stream()
.filter(f -> f.getClass().isAssignableFrom(filterType))
.findFirst()
.orElse(null);
return (T) filter;
}
} }