public static ManagementServerPort get(Environment environment, BeanFactory beanFactory) { Integer serverPort = getPortProperty(environment, "server."); if (serverPort == null && hasCustomBeanDefinition(beanFactory, ServerProperties.class, ServerPropertiesAutoConfiguration.class)) { serverPort = getTemporaryBean(beanFactory, ServerProperties.class) .getPort(); } Integer managementPort = getPortProperty(environment, "management."); if (managementPort == null && hasCustomBeanDefinition(beanFactory, ManagementServerProperties.class, ManagementServerPropertiesAutoConfiguration.class)) { managementPort = getTemporaryBean(beanFactory, ManagementServerProperties.class).getPort(); } if (managementPort != null && managementPort < 0) { return DISABLE; } return ((managementPort == null) || (serverPort == null && managementPort.equals(8080)) || (managementPort != 0 && managementPort.equals(serverPort)) ? SAME : DIFFERENT); }
@Test public void contextPath() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.contextPath:/test"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/test/endpoint", ports.get().server, "endpointoutput"); }
@Test public void overrideServerProperties() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.displayName:foo"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); ServerProperties serverProperties = this.applicationContext .getBean(ServerProperties.class); assertThat(serverProperties.getDisplayName()).isEqualTo("foo"); }
@Test public void testOverrideAuthenticationManagerWithBuilderAndInjectIntoSecurityFilter() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, SecurityCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( "foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); assertThat(this.context.getBean(AuthenticationManager.class).authenticate(user)) .isNotNull(); pingAuthenticationListener(); }
@Test public void testOverrideAuthenticationManagerWithBuilderAndInjectBuilderIntoSecurityFilter() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, WorkaroundSecurityCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( "foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); assertThat(this.context.getBean(AuthenticationManager.class).authenticate(user)) .isNotNull(); }
@Test public void testJpaCoexistsHappily() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.url:jdbc:hsqldb:mem:testsecdb"); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:false"); this.context.register(EntityConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); // This can fail if security @Conditionals force early instantiation of the // HibernateJpaAutoConfiguration (e.g. the EntityManagerFactory is not found) this.context.refresh(); assertThat(this.context.getBean(JpaTransactionManager.class)).isNotNull(); }
@Test public void testDefaultUsernamePassword() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context.refresh(); SecurityProperties security = this.context.getBean(SecurityProperties.class); AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( security.getUser().getName(), security.getUser().getPassword()); assertThat(manager.authenticate(token)).isNotNull(); }
@Test public void testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context.refresh(); SecurityProperties security = this.context.getBean(SecurityProperties.class); AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( security.getUser().getName(), security.getUser().getPassword()); try { manager.authenticate(token); fail("Expected Exception"); } catch (AuthenticationException success) { // Expected } token = new UsernamePasswordAuthenticationToken("foo", "bar"); assertThat(manager.authenticate(token)).isNotNull(); }
@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); }
@Test public void registerWithSimpleWebApp() throws Exception { this.context = new SpringApplicationBuilder() .sources(EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class) .run("--" + ENABLE_ADMIN_PROP, "--server.port=0"); assertThat(this.context).isInstanceOf(EmbeddedWebApplicationContext.class); assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication")).isEqualTo(Boolean.TRUE); int expected = ((EmbeddedWebApplicationContext) this.context) .getEmbeddedServletContainer().getPort(); String actual = getProperty(createDefaultObjectName(), "local.server.port"); assertThat(actual).isEqualTo(String.valueOf(expected)); }
@Test public void contextPath() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.contextPath:/test"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/test/endpoint", ports.get().server, "endpointoutput"); this.applicationContext.close(); assertAllClosed(); }
@Test public void testOverrideAuthenticationManagerWithBuilderAndInjectIntoSecurityFilter() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, SecurityCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( "foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); assertNotNull( this.context.getBean(AuthenticationManager.class).authenticate(user)); pingAuthenticationListener(); }
@Test public void testOverrideAuthenticationManagerWithBuilderAndInjectBuilderIntoSecurityFilter() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, WorkaroundSecurityCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( "foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); assertNotNull( this.context.getBean(AuthenticationManager.class).authenticate(user)); }
@Test public void testJpaCoexistsHappily() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.url:jdbc:hsqldb:mem:testsecdb"); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:false"); this.context.register(EntityConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); // This can fail if security @Conditionals force early instantiation of the // HibernateJpaAutoConfiguration (e.g. the EntityManagerFactory is not found) this.context.refresh(); assertNotNull(this.context.getBean(JpaTransactionManager.class)); }
@Test public void testDefaultUsernamePassword() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context.refresh(); SecurityProperties security = this.context.getBean(SecurityProperties.class); AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( security.getUser().getName(), security.getUser().getPassword()); assertNotNull(manager.authenticate(token)); }
@Test public void testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationManagerCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context.refresh(); SecurityProperties security = this.context.getBean(SecurityProperties.class); AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( security.getUser().getName(), security.getUser().getPassword()); try { manager.authenticate(token); fail("Expected Exception"); } catch (AuthenticationException success) { // Expected } token = new UsernamePasswordAuthenticationToken("foo", "bar"); assertNotNull(manager.authenticate(token)); }
@Test public void registerWithSimpleWebApp() throws Exception { this.context = new SpringApplicationBuilder() .sources(EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class) .run("--" + ENABLE_ADMIN_PROP, "--server.port=0"); assertTrue(this.context instanceof EmbeddedWebApplicationContext); assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication")); int expected = ((EmbeddedWebApplicationContext) this.context) .getEmbeddedServletContainer().getPort(); String actual = getProperty(createDefaultObjectName(), "local.server.port"); assertEquals(String.valueOf(expected), actual); }
@Test public void doesNotEarlyInitializeFactoryBeans() throws Exception { SpringApplication application = new SpringApplication(EarlyInitConfig.class, PropertySourcesPlaceholderConfigurer.class, EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context = application.run("--server.port=0"); String bean = (String) this.context.getBean("earlyInit"); assertThat(bean).isEqualTo("bucket"); }
@Test public void testWebConfiguration() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(AuthenticationManagerBuilder.class)).isNotNull(); // 5 for static resources and one for the rest assertThat(this.context.getBean(FilterChainProxy.class).getFilterChains()) .hasSize(6); }
@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 testFilterIsNotRegisteredInNonWeb() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); try { context.refresh(); assertThat(context.containsBean("securityFilterChainRegistration")).isFalse(); } finally { context.close(); } }
@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); }
@Test public void testDisableIgnoredStaticApplicationPaths() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none"); this.context.refresh(); // Just the application endpoints now assertThat(this.context.getBean(FilterChainProxy.class).getFilterChains()) .hasSize(1); }
@Test public void testDisableBasicAuthOnApplicationPaths() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false"); this.context.refresh(); // Ignores and the "matches-none" filter only assertThat(this.context.getBeanNamesForType(FilterChainProxy.class).length) .isEqualTo(1); }
@Test public void testAuthenticationManagerCreated() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(AuthenticationManager.class)).isNotNull(); }
@Test public void testOverrideAuthenticationManager() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(TestAuthenticationConfiguration.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(AuthenticationManager.class)) .isEqualTo(this.context.getBean( TestAuthenticationConfiguration.class).authenticationManager); }
@Test public void testDefaultAuthenticationManagerMakesUserDetailsAvailable() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(UserDetailsSecurityCustomizer.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(UserDetailsSecurityCustomizer.class) .getUserDetails().loadUserByUsername("user")).isNotNull(); }