@Test public void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() throws Exception { addEmbeddedServletContainerFactoryBean(); DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean( "filterBean"); this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer)); BeanDefinition filterBeanDefinition = beanDefinition( new IllegalStateException("Create FilterBean Failure")); filterBeanDefinition.setLazyInit(true); this.context.registerBeanDefinition("filterBean", filterBeanDefinition); this.context.refresh(); ServletContext servletContext = getEmbeddedServletContainerFactory() .getServletContext(); verify(servletContext, atMost(1)).addFilter(anyString(), this.filterCaptor.capture()); // Up to this point the filterBean should not have been created, calling // the delegate proxy will trigger creation and an exception this.thrown.expect(BeanCreationException.class); this.thrown.expectMessage("Create FilterBean Failure"); this.filterCaptor.getValue().init(new MockFilterConfig()); this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain()); }
@Test public void defaultFilterDispatcherTypes() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DelegatingFilterProxyRegistrationBean bean = this.context.getBean( "securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class); @SuppressWarnings("unchecked") EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils .getField(bean, "dispatcherTypes"); assertThat(dispatcherTypes).isNull(); }
@Test public void customFilterDispatcherTypes() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "security.filter-dispatcher-types:INCLUDE,ERROR"); this.context.refresh(); DelegatingFilterProxyRegistrationBean bean = this.context.getBean( "securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class); @SuppressWarnings("unchecked") EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils .getField(bean, "dispatcherTypes"); assertThat(dispatcherTypes).containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR); }
@Bean DelegatingFilterProxyRegistrationBean delegatingFilterProxyRegistrationBean() { DelegatingFilterProxyRegistrationBean bean = new DelegatingFilterProxyRegistrationBean("traceLoggingFilter"); bean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST); bean.setOrder(Ordered.LOWEST_PRECEDENCE); return bean; }
private void addFilters(ConfigurableMockMvcBuilder<?> builder) { ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans( this.context); for (ServletContextInitializer initializer : Initializers) { if (initializer instanceof FilterRegistrationBean) { addFilter(builder, (FilterRegistrationBean) initializer); } if (initializer instanceof DelegatingFilterProxyRegistrationBean) { addFilter(builder, (DelegatingFilterProxyRegistrationBean) initializer); } } }
@Bean @ConditionalOnBean(name = DEFAULT_FILTER_NAME) public DelegatingFilterProxyRegistrationBean securityFilterChainRegistration( SecurityProperties securityProperties) { DelegatingFilterProxyRegistrationBean registration = new DelegatingFilterProxyRegistrationBean( DEFAULT_FILTER_NAME); registration.setOrder(securityProperties.getFilterOrder()); registration.setDispatcherTypes(getDispatcherTypes(securityProperties)); return registration; }
@Test public void testDefaultFilterOrderWithSecurityAdapter() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(WebSecurity.class, SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class).getOrder()).isEqualTo( FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100); }
@Test public void testDefaultFilterOrder() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class).getOrder()).isEqualTo( FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER - 100); }
@Test public void testCustomFilterOrder() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "security.filter-order:12345"); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class).getOrder()).isEqualTo(12345); }
@Bean public DelegatingFilterProxyRegistrationBean userInsertingMdcFilterRegistrationBean() { DelegatingFilterProxyRegistrationBean registrationBean = new DelegatingFilterProxyRegistrationBean("hystrixRequestContextEnablerFilter", new ServletRegistrationBean[0]); registrationBean.setOrder(Integer.MAX_VALUE - 1); return registrationBean; }
@Bean public DelegatingFilterProxyRegistrationBean setterInsertingMdcFilterRegistrationBean() { DelegatingFilterProxyRegistrationBean registrationBean = new DelegatingFilterProxyRegistrationBean("securityContextHystrixRequestVariableSetterFilter", new ServletRegistrationBean[0]); registrationBean.setOrder(Integer.MAX_VALUE); return registrationBean; }
private void addFilter(ConfigurableMockMvcBuilder<?> builder, DelegatingFilterProxyRegistrationBean registration) { addFilter(builder, registration.getFilter(), registration.getUrlPatterns()); }