1 package cn.home1.oss.environment.admin; 2 3 import de.codecentric.boot.admin.config.AdminServerProperties; 4 import de.codecentric.boot.admin.config.EnableAdminServer; 5 import de.codecentric.boot.admin.registry.StatusUpdater; 6 import de.codecentric.boot.admin.registry.store.ApplicationStore; 7 import de.codecentric.boot.admin.web.client.ApplicationOperations; 8 9 import lombok.extern.slf4j.Slf4j; 10 11 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.boot.SpringApplication; 13 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 14 import org.springframework.boot.autoconfigure.SpringBootApplication; 15 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 16 import org.springframework.boot.context.properties.EnableConfigurationProperties; 17 import org.springframework.boot.web.client.RestTemplateBuilder; 18 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 19 import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 20 import org.springframework.context.ApplicationContext; 21 import org.springframework.context.ApplicationListener; 22 import org.springframework.context.annotation.Bean; 23 import org.springframework.context.annotation.Configuration; 24 import org.springframework.context.annotation.Primary; 25 import org.springframework.context.event.ContextRefreshedEvent; 26 import org.springframework.core.env.Environment; 27 import org.springframework.http.HttpStatus; 28 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 29 import org.springframework.web.client.DefaultResponseErrorHandler; 30 import org.springframework.web.client.RestTemplate; 31 32 import cn.home1.oss.boot.autoconfigure.AppProperties; 33 import cn.home1.oss.lib.common.crypto.Cryptos; 34 import cn.home1.oss.lib.common.crypto.EncodeDecryptor; 35 import cn.home1.oss.lib.common.crypto.KeyExpression; 36 37 @Configuration 38 @EnableAutoConfiguration 39 @EnableAdminServer 40 @EnableDiscoveryClient 41 @EnableZuulProxy 42 @EnableConfigurationProperties(value = {AppProperties.class}) 43 @SpringBootApplication 44 @Slf4j 45 public class AdminApplication implements ApplicationListener<ContextRefreshedEvent> { 46 47 @Autowired 48 private Environment environment; 49 50 @Autowired 51 private ApplicationContext applicationContext; 52 53 @Autowired 54 private ApplicationOperations applicationOperations; 55 56 @Autowired 57 private AdminServerProperties adminServerProperties; 58 59 @Autowired 60 private ApplicationStore applicationStore; 61 62 public static void main(final String[] args) { 63 SpringApplication.run(AdminApplication.class, args); 64 } 65 66 @Bean 67 @ConditionalOnMissingBean 68 public ClientKeyStore clientKeyStoreListener() { 69 return new ClientKeyStore(); 70 } 71 72 @Bean 73 public BasicAuthFilter basicAuthFilter() { 74 return new BasicAuthFilter(); 75 } 76 77 @Bean 78 public PostFilterForClientInfo postFilterForClientInfo() { 79 return new PostFilterForClientInfo(); 80 } 81 82 @Bean 83 public RestTemplate restTemplate(final RestTemplateBuilder builder) { 84 return builder.build(); 85 } 86 87 @Bean 88 public EncodeDecryptor decryptor() { 89 final String adminPrivateKey = this.environment.getProperty("app.adminPrivateKey"); 90 return Cryptos.decryptor(new KeyExpression(adminPrivateKey)); 91 } 92 93 @Primary 94 @Bean 95 @ConditionalOnMissingBean 96 public StatusUpdater statusUpdater() { 97 98 log.info("Registering patched version of de.codecentric.boot.admin.registry.StatusUpdates"); 99 100 final RestTemplate restTemplate = new RestTemplate(); 101 restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 102 restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { 103 @Override 104 protected boolean hasError(final HttpStatus statusCode) { 105 return false; 106 } 107 }); 108 109 final StatusUpdater statusUpdater = new PatchedStatusUpdater(this.applicationStore, this.applicationOperations, restTemplate); 110 statusUpdater.setStatusLifetime(this.adminServerProperties.getMonitor().getStatusLifetime()); 111 112 return statusUpdater; 113 } 114 115 @Override 116 public void onApplicationEvent(final ContextRefreshedEvent event) { 117 final PatchedStatusUpdater statusUpdater = (PatchedStatusUpdater) this.applicationContext.getBean("statusUpdater"); 118 statusUpdater.setActive(true); 119 } 120 }