|
36 | 36 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
37 | 37 |
|
38 | 38 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 39 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
39 | 40 |
|
40 | 41 | import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent; |
41 | 42 | import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent; |
42 | 43 | import de.codecentric.boot.admin.event.RoutesOutdatedEvent; |
| 44 | +import de.codecentric.boot.admin.jackson.ApplicationBeanSerializerModifier; |
| 45 | +import de.codecentric.boot.admin.jackson.ApplicationDeserializer; |
| 46 | +import de.codecentric.boot.admin.jackson.SanitizingMapSerializer; |
43 | 47 | import de.codecentric.boot.admin.journal.ApplicationEventJournal; |
44 | 48 | import de.codecentric.boot.admin.journal.web.JournalController; |
| 49 | +import de.codecentric.boot.admin.model.Application; |
45 | 50 | import de.codecentric.boot.admin.registry.ApplicationRegistry; |
46 | 51 | import de.codecentric.boot.admin.registry.web.RegistryController; |
47 | 52 | import de.codecentric.boot.admin.web.AdminController; |
|
51 | 56 | import de.codecentric.boot.admin.web.servlet.resource.ResourcePatternResolvingResourceResolver; |
52 | 57 |
|
53 | 58 | @Configuration |
54 | | -public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter |
55 | | - implements ApplicationContextAware { |
56 | | - private final ApplicationEventPublisher publisher; |
57 | | - private final ServerProperties server; |
58 | | - private final ResourcePatternResolver resourcePatternResolver; |
59 | | - private final AdminServerProperties adminServerProperties; |
60 | | - private ApplicationContext applicationContext; |
61 | | - |
62 | | - public AdminServerWebConfiguration(ApplicationEventPublisher publisher, ServerProperties server, |
63 | | - ResourcePatternResolver resourcePatternResolver, |
64 | | - AdminServerProperties adminServerProperties) { |
65 | | - this.publisher = publisher; |
66 | | - this.server = server; |
67 | | - this.resourcePatternResolver = resourcePatternResolver; |
68 | | - this.adminServerProperties = adminServerProperties; |
69 | | - } |
70 | | - |
71 | | - @Override |
72 | | - public void setApplicationContext(ApplicationContext applicationContext) { |
73 | | - this.applicationContext = applicationContext; |
74 | | - } |
75 | | - |
76 | | - @Override |
77 | | - public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { |
78 | | - if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) { |
79 | | - ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() |
80 | | - .applicationContext(this.applicationContext).build(); |
81 | | - converters.add(new MappingJackson2HttpMessageConverter(objectMapper)); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - private boolean hasConverter(List<HttpMessageConverter<?>> converters, |
86 | | - Class<? extends HttpMessageConverter<?>> clazz) { |
87 | | - for (HttpMessageConverter<?> converter : converters) { |
88 | | - if (clazz.isInstance(converter)) { |
89 | | - return true; |
90 | | - } |
91 | | - } |
92 | | - return false; |
93 | | - } |
94 | | - |
95 | | - @Override |
96 | | - public void addResourceHandlers(ResourceHandlerRegistry registry) { |
97 | | - registry.addResourceHandler(adminServerProperties.getContextPath() + "/**") |
98 | | - .addResourceLocations("classpath:/META-INF/spring-boot-admin-server-ui/") |
99 | | - .resourceChain(true) |
100 | | - .addResolver(new PreferMinifiedFilteringResourceResolver(".min")); |
101 | | - |
102 | | - registry.addResourceHandler(adminServerProperties.getContextPath() + "/all-modules.css") |
103 | | - .resourceChain(true) |
104 | | - .addResolver(new ResourcePatternResolvingResourceResolver(resourcePatternResolver, |
105 | | - "classpath*:/META-INF/spring-boot-admin-server-ui/*/module.css")) |
106 | | - .addResolver(new ConcatenatingResourceResolver("\n".getBytes())); |
107 | | - |
108 | | - registry.addResourceHandler(adminServerProperties.getContextPath() + "/all-modules.js") |
109 | | - .resourceChain(true) |
110 | | - .addResolver(new ResourcePatternResolvingResourceResolver(resourcePatternResolver, |
111 | | - "classpath*:/META-INF/spring-boot-admin-server-ui/*/module.js")) |
112 | | - .addResolver(new PreferMinifiedFilteringResourceResolver(".min")) |
113 | | - .addResolver(new ConcatenatingResourceResolver(";\n".getBytes())); |
114 | | - } |
115 | | - |
116 | | - @Override |
117 | | - public void addViewControllers(ViewControllerRegistry registry) { |
118 | | - String contextPath = adminServerProperties.getContextPath(); |
119 | | - if (StringUtils.hasText(contextPath)) { |
120 | | - registry.addRedirectViewController(contextPath, server.getPath(contextPath) + "/"); |
121 | | - } |
122 | | - registry.addViewController(contextPath + "/").setViewName("forward:index.html"); |
123 | | - } |
124 | | - |
125 | | - @Bean |
126 | | - public PrefixHandlerMapping prefixHandlerMapping() { |
127 | | - Map<String, Object> beans = applicationContext |
128 | | - .getBeansWithAnnotation(AdminController.class); |
129 | | - PrefixHandlerMapping prefixHandlerMapping = new PrefixHandlerMapping( |
130 | | - beans.values().toArray(new Object[beans.size()])); |
131 | | - prefixHandlerMapping.setPrefix(adminServerProperties.getContextPath()); |
132 | | - return prefixHandlerMapping; |
133 | | - } |
134 | | - |
135 | | - @Bean |
136 | | - @ConditionalOnMissingBean |
137 | | - public RegistryController registryController(ApplicationRegistry applicationRegistry) { |
138 | | - return new RegistryController(applicationRegistry); |
139 | | - } |
140 | | - |
141 | | - @Bean |
142 | | - @ConditionalOnMissingBean |
143 | | - public JournalController journalController(ApplicationEventJournal applicationEventJournal) { |
144 | | - return new JournalController(applicationEventJournal); |
145 | | - } |
146 | | - |
147 | | - @EventListener |
148 | | - public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) { |
149 | | - publisher.publishEvent(new RoutesOutdatedEvent()); |
150 | | - } |
151 | | - |
152 | | - @EventListener |
153 | | - public void onClientApplicationDeregistered(ClientApplicationDeregisteredEvent event) { |
154 | | - publisher.publishEvent(new RoutesOutdatedEvent()); |
155 | | - } |
| 59 | +public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware { |
| 60 | + private final ApplicationEventPublisher publisher; |
| 61 | + private final ServerProperties server; |
| 62 | + private final ResourcePatternResolver resourcePatternResolver; |
| 63 | + private final AdminServerProperties adminServerProperties; |
| 64 | + private ApplicationContext applicationContext; |
| 65 | + |
| 66 | + public AdminServerWebConfiguration(ApplicationEventPublisher publisher, ServerProperties server, ResourcePatternResolver resourcePatternResolver, AdminServerProperties adminServerProperties) { |
| 67 | + this.publisher = publisher; |
| 68 | + this.server = server; |
| 69 | + this.resourcePatternResolver = resourcePatternResolver; |
| 70 | + this.adminServerProperties = adminServerProperties; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 75 | + this.applicationContext = applicationContext; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { |
| 80 | + if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) { |
| 81 | + ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() |
| 82 | + .applicationContext(this.applicationContext) |
| 83 | + .build(); |
| 84 | + converters.add(new MappingJackson2HttpMessageConverter(objectMapper)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private boolean hasConverter(List<HttpMessageConverter<?>> converters, Class<? extends HttpMessageConverter<?>> clazz) { |
| 89 | + for (HttpMessageConverter<?> converter : converters) { |
| 90 | + if (clazz.isInstance(converter)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 99 | + registry.addResourceHandler(adminServerProperties.getContextPath() + "/**") |
| 100 | + .addResourceLocations("classpath:/META-INF/spring-boot-admin-server-ui/") |
| 101 | + .resourceChain(true) |
| 102 | + .addResolver(new PreferMinifiedFilteringResourceResolver(".min")); |
| 103 | + |
| 104 | + registry.addResourceHandler(adminServerProperties.getContextPath() + "/all-modules.css") |
| 105 | + .resourceChain(true) |
| 106 | + .addResolver(new ResourcePatternResolvingResourceResolver(resourcePatternResolver, |
| 107 | + "classpath*:/META-INF/spring-boot-admin-server-ui/*/module.css")) |
| 108 | + .addResolver(new ConcatenatingResourceResolver("\n".getBytes())); |
| 109 | + |
| 110 | + registry.addResourceHandler(adminServerProperties.getContextPath() + "/all-modules.js") |
| 111 | + .resourceChain(true) |
| 112 | + .addResolver(new ResourcePatternResolvingResourceResolver(resourcePatternResolver, |
| 113 | + "classpath*:/META-INF/spring-boot-admin-server-ui/*/module.js")) |
| 114 | + .addResolver(new PreferMinifiedFilteringResourceResolver(".min")) |
| 115 | + .addResolver(new ConcatenatingResourceResolver(";\n".getBytes())); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void addViewControllers(ViewControllerRegistry registry) { |
| 120 | + String contextPath = adminServerProperties.getContextPath(); |
| 121 | + if (StringUtils.hasText(contextPath)) { |
| 122 | + registry.addRedirectViewController(contextPath, server.getPath(contextPath) + "/"); |
| 123 | + } |
| 124 | + registry.addViewController(contextPath + "/").setViewName("forward:index.html"); |
| 125 | + } |
| 126 | + |
| 127 | + @Bean |
| 128 | + public SimpleModule adminJacksonModule() { |
| 129 | + SimpleModule module = new SimpleModule(); |
| 130 | + module.addDeserializer(Application.class, new ApplicationDeserializer()); |
| 131 | + module.setSerializerModifier(new ApplicationBeanSerializerModifier( |
| 132 | + new SanitizingMapSerializer(adminServerProperties.getMetadataKeysToSanitize()))) ; |
| 133 | + return module; |
| 134 | + } |
| 135 | + |
| 136 | + @Bean |
| 137 | + public PrefixHandlerMapping prefixHandlerMapping() { |
| 138 | + Map<String, Object> beans = applicationContext.getBeansWithAnnotation(AdminController.class); |
| 139 | + PrefixHandlerMapping prefixHandlerMapping = new PrefixHandlerMapping( |
| 140 | + beans.values().toArray(new Object[beans.size()])); |
| 141 | + prefixHandlerMapping.setPrefix(adminServerProperties.getContextPath()); |
| 142 | + return prefixHandlerMapping; |
| 143 | + } |
| 144 | + |
| 145 | + @Bean |
| 146 | + @ConditionalOnMissingBean |
| 147 | + public RegistryController registryController(ApplicationRegistry applicationRegistry) { |
| 148 | + return new RegistryController(applicationRegistry); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + @ConditionalOnMissingBean |
| 153 | + public JournalController journalController(ApplicationEventJournal applicationEventJournal) { |
| 154 | + return new JournalController(applicationEventJournal); |
| 155 | + } |
| 156 | + |
| 157 | + @EventListener |
| 158 | + public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) { |
| 159 | + publisher.publishEvent(new RoutesOutdatedEvent()); |
| 160 | + } |
| 161 | + |
| 162 | + @EventListener |
| 163 | + public void onClientApplicationDeregistered(ClientApplicationDeregisteredEvent event) { |
| 164 | + publisher.publishEvent(new RoutesOutdatedEvent()); |
| 165 | + } |
156 | 166 |
|
157 | 167 | } |
0 commit comments