public String[] getPaths(EndpointHandlerMapping endpointHandlerMapping) { if (endpointHandlerMapping == null) { return NO_PATHS; } Set<? extends MvcEndpoint> endpoints = endpointHandlerMapping.getEndpoints(); Set<String> paths = new LinkedHashSet<String>(endpoints.size()); for (MvcEndpoint endpoint : endpoints) { if (isIncluded(endpoint)) { String path = endpointHandlerMapping.getPath(endpoint.getPath()); paths.add(path); if (!path.equals("")) { if (endpoint.isSensitive()) { // Ensure that nested paths are secured paths.add(path + "/**"); // Add Spring MVC-generated additional paths paths.add(path + ".*"); } } paths.add(path + "/"); } } return paths.toArray(new String[paths.size()]); }
@Bean @ConditionalOnMissingBean public EndpointHandlerMapping endpointHandlerMapping() { Set<? extends MvcEndpoint> endpoints = mvcEndpoints().getEndpoints(); CorsConfiguration corsConfiguration = getCorsConfiguration(this.corsProperties); EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints, corsConfiguration); boolean disabled = this.managementServerProperties.getPort() != null && this.managementServerProperties.getPort() == -1; mapping.setDisabled(disabled); if (!disabled) { mapping.setPrefix(this.managementServerProperties.getContextPath()); } if (this.mappingCustomizers != null) { for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) { customizer.customize(mapping); } } return mapping; }
@Test public void onDifferentPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); }
@Test public void onDifferentPortWithSpecificContainer() throws Exception { this.applicationContext.register(SpecificContainerConfig.class, RootConfig.class, DifferentPortConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); EmbeddedServletContainerFactory parentContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); EmbeddedServletContainerFactory managementContainerFactory = managementContext .getBean(EmbeddedServletContainerFactory.class); assertThat(parentContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory).isNotSameAs(parentContainerFactory); }
@Test public void managementSpecificSslUsingDifferentPort() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.ssl.enabled=true", "management.ssl.key-store=classpath:test.jks", "management.ssl.key-password=password"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertHttpsContent("/controller", ports.get().management, null); assertHttpsContent("/endpoint", ports.get().management, "endpointoutput"); assertHttpsContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); ManagementServerProperties managementServerProperties = this.applicationContext .getBean(ManagementServerProperties.class); assertThat(managementServerProperties.getSsl()).isNotNull(); assertThat(managementServerProperties.getSsl().isEnabled()).isTrue(); }
@Test public void managementServerCanDisableSslWhenUsingADifferentPort() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.ssl.enabled=true", "server.ssl.key-store=classpath:test.jks", "server.ssl.key-password=password", "management.ssl.enabled=false"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertHttpsContent("/controller", ports.get().server, "controlleroutput"); assertHttpsContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); ManagementServerProperties managementServerProperties = this.applicationContext .getBean(ManagementServerProperties.class); assertThat(managementServerProperties.getSsl()).isNotNull(); assertThat(managementServerProperties.getSsl().isEnabled()).isFalse(); }
@Test public void beanMethodMappings() { StaticApplicationContext context = new StaticApplicationContext(); EndpointHandlerMapping mapping = new EndpointHandlerMapping( Arrays.asList(new EndpointMvcAdapter(new DumpEndpoint()))); mapping.setApplicationContext(new StaticApplicationContext()); mapping.afterPropertiesSet(); context.getDefaultListableBeanFactory().registerSingleton("mapping", mapping); this.endpoint.setApplicationContext(context); Map<String, Object> result = this.endpoint.invoke(); assertThat(result).hasSize(1); assertThat(result.keySet().iterator().next().contains("/dump")).isTrue(); @SuppressWarnings("unchecked") Map<String, Object> handler = (Map<String, Object>) result.values().iterator() .next(); assertThat(handler.containsKey("method")).isTrue(); }
@Test public void concreteMethodMappings() { EndpointHandlerMapping mapping = new EndpointHandlerMapping( Arrays.asList(new EndpointMvcAdapter(new DumpEndpoint()))); mapping.setApplicationContext(new StaticApplicationContext()); mapping.afterPropertiesSet(); this.endpoint.setMethodMappings( Collections.<AbstractHandlerMethodMapping<?>>singletonList(mapping)); Map<String, Object> result = this.endpoint.invoke(); assertThat(result).hasSize(1); assertThat(result.keySet().iterator().next().contains("/dump")).isTrue(); @SuppressWarnings("unchecked") Map<String, Object> handler = (Map<String, Object>) result.values().iterator() .next(); assertThat(handler.containsKey("method")).isTrue(); }
protected final EndpointHandlerMapping getRequiredEndpointHandlerMapping() { if (this.endpointHandlerMapping == null) { ApplicationContext context = (this.contextResolver == null ? null : this.contextResolver.getApplicationContext()); if (context != null && context .getBeanNamesForType(EndpointHandlerMapping.class).length > 0) { this.endpointHandlerMapping = context .getBean(EndpointHandlerMapping.class); } if (this.endpointHandlerMapping == null) { this.endpointHandlerMapping = new EndpointHandlerMapping( Collections.<MvcEndpoint>emptySet()); } } return this.endpointHandlerMapping; }
@Override protected void postProcessMapping(ListableBeanFactory beanFactory, EndpointHandlerMapping mapping) { // The parent context has the security filter, so we need to get it injected // with our EndpointHandlerMapping if we can. if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, ManagementWebSecurityConfigurerAdapter.class).length == 1) { ManagementWebSecurityConfigurerAdapter bean = beanFactory .getBean(ManagementWebSecurityConfigurerAdapter.class); bean.setEndpointHandlerMapping(mapping); } else { logger.warn("No single bean of type " + ManagementWebSecurityConfigurerAdapter.class.getSimpleName() + " found (this might make some endpoints inaccessible without authentication)"); } }
@Test public void onDifferentPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertEquals(1, interceptors.size()); this.applicationContext.close(); assertAllClosed(); }
@Test public void beanMethodMappings() { StaticApplicationContext context = new StaticApplicationContext(); EndpointHandlerMapping mapping = new EndpointHandlerMapping( Arrays.asList(new EndpointMvcAdapter(new DumpEndpoint()))); mapping.setApplicationContext(new StaticApplicationContext()); mapping.afterPropertiesSet(); context.getDefaultListableBeanFactory().registerSingleton("mapping", mapping); this.endpoint.setApplicationContext(context); Map<String, Object> result = this.endpoint.invoke(); assertEquals(1, result.size()); assertTrue(result.keySet().iterator().next().contains("/dump")); @SuppressWarnings("unchecked") Map<String, Object> handler = (Map<String, Object>) result.values().iterator() .next(); assertTrue(handler.containsKey("method")); }
@Test public void concreteMethodMappings() { EndpointHandlerMapping mapping = new EndpointHandlerMapping( Arrays.asList(new EndpointMvcAdapter(new DumpEndpoint()))); mapping.setApplicationContext(new StaticApplicationContext()); mapping.afterPropertiesSet(); this.endpoint.setMethodMappings( Collections.<AbstractHandlerMethodMapping<?>>singletonList(mapping)); Map<String, Object> result = this.endpoint.invoke(); assertEquals(1, result.size()); assertTrue(result.keySet().iterator().next().contains("/dump")); @SuppressWarnings("unchecked") Map<String, Object> handler = (Map<String, Object>) result.values().iterator() .next(); assertTrue(handler.containsKey("method")); }
private RequestMatcher createDelegate() { ServerProperties server = this.contextResolver.getApplicationContext() .getBean(ServerProperties.class); List<RequestMatcher> matchers = new ArrayList<RequestMatcher>(); EndpointHandlerMapping endpointHandlerMapping = getRequiredEndpointHandlerMapping(); for (String path : this.endpointPaths.getPaths(endpointHandlerMapping)) { matchers.add(new AntPathRequestMatcher(server.getPath(path))); } return (matchers.isEmpty() ? MATCH_NONE : new OrRequestMatcher(matchers)); }
@Bean public EndpointHandlerMappingCustomizer mappingCustomizer() { return new EndpointHandlerMappingCustomizer() { @Override public void customize(EndpointHandlerMapping mapping) { mapping.setInterceptors(new Object[] { interceptor() }); } }; }
@Test public void singleRequestMappingInfoHandlerMappingBean() throws Exception { this.applicationContext.register(RootConfig.class, BaseConfiguration.class, ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); RequestMappingInfoHandlerMapping mapping = this.applicationContext .getBean(RequestMappingInfoHandlerMapping.class); assertThat(mapping).isNotEqualTo(instanceOf(EndpointHandlerMapping.class)); }
@Test public void onDifferentPortWithSpecificContainer() throws Exception { this.applicationContext.register(SpecificContainerConfig.class, RootConfig.class, DifferentPortConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); EmbeddedServletContainerFactory parentContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); EmbeddedServletContainerFactory managementContainerFactory = managementContext .getBean(EmbeddedServletContainerFactory.class); assertThat(parentContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory).isNotSameAs(parentContainerFactory); this.applicationContext.close(); assertAllClosed(); }
private RequestMatcher createDelegate() { ServerProperties server = ManagementWebSecurityConfigurerAdapter.this.server; List<RequestMatcher> matchers = new ArrayList<RequestMatcher>(); EndpointHandlerMapping endpointHandlerMapping = ManagementWebSecurityConfigurerAdapter.this .getRequiredEndpointHandlerMapping(); for (String path : this.endpointPaths.getPaths(endpointHandlerMapping)) { matchers.add(new AntPathRequestMatcher(server.getPath(path))); } return (matchers.isEmpty() ? AnyRequestMatcher.INSTANCE : new OrRequestMatcher(matchers)); }
@Autowired public void handlerMapping(MvcEndpoints endpoints, ListableBeanFactory beanFactory, EndpointHandlerMapping mapping) { // In a child context we definitely want to see the parent endpoints mapping.setDetectHandlerMethodsInAncestorContexts(true); postProcessMapping(beanFactory, mapping); }
@Test public void singleRequestMappingInfoHandlerMappingBean() throws Exception { this.applicationContext.register(RootConfig.class, BaseConfiguration.class, ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); RequestMappingInfoHandlerMapping mapping = this.applicationContext .getBean(RequestMappingInfoHandlerMapping.class); assertThat(mapping, not(instanceOf(EndpointHandlerMapping.class))); }
@Autowired public void handlerMapping(MvcEndpoints endpoints, ListableBeanFactory beanFactory, EndpointHandlerMapping mapping) { // In a child context we definitely want to see the parent endpoints mapping.setDetectHandlerMethodsInAncestorContexts(true); }
@Bean public EndpointHandlerMapping endpointHandlerMapping( Collection<? extends MvcEndpoint> endpoints) { return new EndpointHandlerMapping(endpoints); }
public void setEndpointHandlerMapping( EndpointHandlerMapping endpointHandlerMapping) { this.endpointHandlerMapping = endpointHandlerMapping; }