From 66f4d44c60ec3c4314739eb6eb544dcf61628dbb Mon Sep 17 00:00:00 2001 From: Amy DeGregorio Date: Mon, 16 Sep 2019 09:37:38 -0400 Subject: [PATCH] BAEL-3222 Example Code and update to spring-boot-admin examples (#7804) --- spring-boot-admin/pom.xml | 2 +- .../spring-boot-admin-client/pom.xml | 4 +- .../src/main/resources/application.properties | 20 ++-- .../spring-boot-admin-server/pom.xml | 18 ++-- .../SpringBootAdminServerApplication.java | 2 +- .../configs/HazelcastConfig.java | 37 ++++++-- .../configs/NotifierConfiguration.java | 34 ++++--- .../configs/WebSecurityConfig.java | 63 +++++++++---- .../src/main/resources/application.properties | 17 ++-- .../NotifierConfigurationIntegrationTest.java | 8 +- spring-boot-management/pom.xml | 91 ++++++++++--------- .../logging/LoggingApplication.java | 11 +++ .../management/logging/LoggingController.java | 24 +++++ .../management/logging/SecurityConfig.java | 14 +++ .../src/main/resources/application.properties | 21 ++++- .../src/main/resources/logback.xml | 13 +++ 16 files changed, 260 insertions(+), 119 deletions(-) create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingApplication.java create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingController.java create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/SecurityConfig.java create mode 100644 spring-boot-management/src/main/resources/logback.xml diff --git a/spring-boot-admin/pom.xml b/spring-boot-admin/pom.xml index 23852dee57..b2ced767eb 100644 --- a/spring-boot-admin/pom.xml +++ b/spring-boot-admin/pom.xml @@ -30,7 +30,7 @@ - 1.5.8.RELEASE + 2.1.7.RELEASE \ No newline at end of file diff --git a/spring-boot-admin/spring-boot-admin-client/pom.xml b/spring-boot-admin/spring-boot-admin-client/pom.xml index 7563a01172..aa2daf8d45 100644 --- a/spring-boot-admin/spring-boot-admin-client/pom.xml +++ b/spring-boot-admin/spring-boot-admin-client/pom.xml @@ -18,7 +18,7 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web org.springframework.boot @@ -59,7 +59,7 @@ - 1.5.4 + 2.1.6 2.0.4.RELEASE diff --git a/spring-boot-admin/spring-boot-admin-client/src/main/resources/application.properties b/spring-boot-admin/spring-boot-admin-client/src/main/resources/application.properties index 58c178ecd9..ff4b1e3255 100644 --- a/spring-boot-admin/spring-boot-admin-client/src/main/resources/application.properties +++ b/spring-boot-admin/spring-boot-admin-client/src/main/resources/application.properties @@ -1,16 +1,20 @@ #basic auth creddentials -security.user.name=client -security.user.password=client +spring.security.user.name=client +spring.security.user.password=client #configs to connect to a secured server -spring.boot.admin.url=http://localhost:8080 -spring.boot.admin.username=admin -spring.boot.admin.password=admin +spring.boot.admin.client.url=http://localhost:8080 +#spring.boot.admin.client.instance.service-base-url=http://localhost:8081 +spring.boot.admin.client.username=admin +spring.boot.admin.client.password=admin #configs to give secured server info -spring.boot.admin.client.metadata.user.name=${security.user.name} -spring.boot.admin.client.metadata.user.password=${security.user.password} +spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name} +spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password} #app config spring.application.name=spring-boot-admin-client -server.port=8081 \ No newline at end of file +server.port=8081 + +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always \ No newline at end of file diff --git a/spring-boot-admin/spring-boot-admin-server/pom.xml b/spring-boot-admin/spring-boot-admin-server/pom.xml index d429d9289f..142cf420f4 100644 --- a/spring-boot-admin/spring-boot-admin-server/pom.xml +++ b/spring-boot-admin/spring-boot-admin-server/pom.xml @@ -18,18 +18,14 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web + de.codecentric - spring-boot-admin-server - ${spring-boot-admin-server.version} - - - de.codecentric - spring-boot-admin-server-ui + spring-boot-admin-starter-server ${spring-boot-admin-server.version} @@ -37,7 +33,7 @@ de.codecentric spring-boot-admin-server-ui-login - ${spring-boot-admin-server.version} + ${spring-boot-admin-server-ui-login.version} org.springframework.boot @@ -47,6 +43,7 @@ com.hazelcast hazelcast + de.codecentric @@ -82,8 +79,9 @@ - 1.5.4 - 1.5.4 + 2.1.6 + 2.1.6 + 1.5.7 2.0.4.RELEASE diff --git a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/SpringBootAdminServerApplication.java b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/SpringBootAdminServerApplication.java index d1fb4e769b..e934086cf4 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/SpringBootAdminServerApplication.java +++ b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/SpringBootAdminServerApplication.java @@ -1,6 +1,6 @@ package com.baeldung.springbootadminserver; -import de.codecentric.boot.admin.config.EnableAdminServer; +import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/HazelcastConfig.java b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/HazelcastConfig.java index b19b7820af..d38b0e933c 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/HazelcastConfig.java +++ b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/HazelcastConfig.java @@ -2,8 +2,14 @@ package com.baeldung.springbootadminserver.configs; import com.hazelcast.config.Config; import com.hazelcast.config.EvictionPolicy; -import com.hazelcast.config.ListConfig; +import com.hazelcast.config.InMemoryFormat; import com.hazelcast.config.MapConfig; +import com.hazelcast.config.MergePolicyConfig; +import com.hazelcast.config.TcpIpConfig; +import com.hazelcast.map.merge.PutIfAbsentMapMergePolicy; + +import java.util.Collections; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -12,13 +18,30 @@ public class HazelcastConfig { @Bean public Config hazelcast() { - return new Config() - .setProperty("hazelcast.jmx", "true") - .addMapConfig(new MapConfig("spring-boot-admin-application-store") + MapConfig eventStoreMap = new MapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) - .setEvictionPolicy(EvictionPolicy.NONE)) - .addListConfig(new ListConfig("spring-boot-admin-event-store") + .setEvictionPolicy(EvictionPolicy.NONE) + .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100)); + + MapConfig sentNotificationsMap = new MapConfig("spring-boot-admin-application-store").setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) - .setMaxSize(1000)); + .setEvictionPolicy(EvictionPolicy.LRU) + .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100)); + + Config config = new Config(); + config.addMapConfig(eventStoreMap); + config.addMapConfig(sentNotificationsMap); + config.setProperty("hazelcast.jmx", "true"); + + config.getNetworkConfig() + .getJoin() + .getMulticastConfig() + .setEnabled(false); + TcpIpConfig tcpIpConfig = config.getNetworkConfig() + .getJoin() + .getTcpIpConfig(); + tcpIpConfig.setEnabled(true); + tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1")); + return config; } } diff --git a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/NotifierConfiguration.java b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/NotifierConfiguration.java index 10a31464ab..d43f4b430a 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/NotifierConfiguration.java +++ b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/NotifierConfiguration.java @@ -1,42 +1,40 @@ package com.baeldung.springbootadminserver.configs; -import de.codecentric.boot.admin.notify.LoggingNotifier; -import de.codecentric.boot.admin.notify.RemindingNotifier; -import de.codecentric.boot.admin.notify.filter.FilteringNotifier; +import java.time.Duration; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import java.util.concurrent.TimeUnit; +import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; +import de.codecentric.boot.admin.server.notify.LoggingNotifier; +import de.codecentric.boot.admin.server.notify.RemindingNotifier; +import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier; @Configuration -@EnableScheduling public class NotifierConfiguration { + @Autowired + private InstanceRepository repository; - // @Autowired private Notifier notifier; + // @Autowired private Notifier notifier; @Bean public LoggingNotifier notifier() { - return new LoggingNotifier(); + return new LoggingNotifier(repository); } @Bean public FilteringNotifier filteringNotifier() { - return new FilteringNotifier(notifier()); + return new FilteringNotifier(notifier(), repository); } - @Bean @Primary + @Bean(initMethod = "start", destroyMethod = "stop") public RemindingNotifier remindingNotifier() { - RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier()); - remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5)); + RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier(), repository); + remindingNotifier.setReminderPeriod(Duration.ofMinutes(5)); + remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(60)); return remindingNotifier; } - - @Scheduled(fixedRate = 60_000L) - public void remind() { - remindingNotifier().sendReminders(); - } } diff --git a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/WebSecurityConfig.java b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/WebSecurityConfig.java index 4a7c8330b7..ee37d2f0b3 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/WebSecurityConfig.java +++ b/spring-boot-admin/spring-boot-admin-server/src/main/java/com/baeldung/springbootadminserver/configs/WebSecurityConfig.java @@ -1,33 +1,56 @@ package com.baeldung.springbootadminserver.configs; +import java.util.UUID; + import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; +import org.springframework.security.web.csrf.CookieCsrfTokenRepository; +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; + +import de.codecentric.boot.admin.server.config.AdminServerProperties; @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + private final AdminServerProperties adminServer; + + public WebSecurityConfig(AdminServerProperties adminServer) { + this.adminServer = adminServer; + } @Override protected void configure(HttpSecurity http) throws Exception { - http - .formLogin() - .loginPage("/login.html") - .loginProcessingUrl("/login") - .permitAll(); - http - .logout() - .logoutUrl("/logout"); - http - .csrf() - .disable(); - http - .authorizeRequests() - .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**") - .permitAll(); - http - .authorizeRequests() - .antMatchers("/**") - .authenticated(); - http.httpBasic(); + SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); + successHandler.setTargetUrlParameter("redirectTo"); + successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/"); + + http.authorizeRequests() + .antMatchers(this.adminServer.getContextPath() + "/assets/**") + .permitAll() + .antMatchers(this.adminServer.getContextPath() + "/login") + .permitAll() + .anyRequest() + .authenticated() + .and() + .formLogin() + .loginPage(this.adminServer.getContextPath() + "/login") + .successHandler(successHandler) + .and() + .logout() + .logoutUrl(this.adminServer.getContextPath() + "/logout") + .and() + .httpBasic() + .and() + .csrf() + .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) + .ignoringRequestMatchers(new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances", HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances/*", HttpMethod.DELETE.toString()), + new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**")) + .and() + .rememberMe() + .key(UUID.randomUUID() + .toString()) + .tokenValiditySeconds(1209600); } } diff --git a/spring-boot-admin/spring-boot-admin-server/src/main/resources/application.properties b/spring-boot-admin/spring-boot-admin-server/src/main/resources/application.properties index 362f6428e8..ba7a7de2a0 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/main/resources/application.properties +++ b/spring-boot-admin/spring-boot-admin-server/src/main/resources/application.properties @@ -1,16 +1,19 @@ spring.application.name=spring-boot-admin-server -security.user.name=admin -security.user.password=admin +spring.security.user.name=admin +spring.security.user.password=admin #configs to connect to self register the admin server as a client -spring.boot.admin.url=http://localhost:8080 -spring.boot.admin.username=${security.user.name} -spring.boot.admin.password=${security.user.password} +spring.boot.admin.client.url=http://localhost:8080 +spring.boot.admin.client.username=${spring.security.user.name} +spring.boot.admin.client.password=${spring.security.user.password} #configs to give secured server info -spring.boot.admin.client.metadata.user.name=${security.user.name} -spring.boot.admin.client.metadata.user.password=${security.user.password} +spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name} +spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password} + +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always #mail notifications #spring.mail.host=smtp.gmail.com diff --git a/spring-boot-admin/spring-boot-admin-server/src/test/java/com/baeldung/springbootadminserver/NotifierConfigurationIntegrationTest.java b/spring-boot-admin/spring-boot-admin-server/src/test/java/com/baeldung/springbootadminserver/NotifierConfigurationIntegrationTest.java index 465d079ac3..fc8acc4e4d 100644 --- a/spring-boot-admin/spring-boot-admin-server/src/test/java/com/baeldung/springbootadminserver/NotifierConfigurationIntegrationTest.java +++ b/spring-boot-admin/spring-boot-admin-server/src/test/java/com/baeldung/springbootadminserver/NotifierConfigurationIntegrationTest.java @@ -1,9 +1,11 @@ package com.baeldung.springbootadminserver; import com.baeldung.springbootadminserver.configs.NotifierConfiguration; -import de.codecentric.boot.admin.notify.Notifier; -import de.codecentric.boot.admin.notify.RemindingNotifier; -import de.codecentric.boot.admin.notify.filter.FilteringNotifier; + +import de.codecentric.boot.admin.server.notify.Notifier; +import de.codecentric.boot.admin.server.notify.RemindingNotifier; +import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-boot-management/pom.xml b/spring-boot-management/pom.xml index 2b3e8f5d06..ca483d2f61 100644 --- a/spring-boot-management/pom.xml +++ b/spring-boot-management/pom.xml @@ -1,47 +1,56 @@ - - 4.0.0 - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../parent-boot-2 - - com.baeldung - spring-boot-management - 0.0.1-SNAPSHOT - spring-boot-management + + 4.0.0 + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + com.baeldung + spring-boot-management + 0.0.1-SNAPSHOT + spring-boot-management - - 1.8 - + + com.baeldung.spring.boot.management.logging.LoggingApplication + 1.8 + 2.1.6 + - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-security + + + de.codecentric + spring-boot-admin-starter-client + ${spring-boot-admin-starter-client.version} + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingApplication.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingApplication.java new file mode 100644 index 0000000000..6371ac4c64 --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.boot.management.logging; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LoggingApplication { + public static void main(String[] args) { + SpringApplication.run(LoggingApplication.class); + } +} diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingController.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingController.java new file mode 100644 index 0000000000..5bd15be10c --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/LoggingController.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.boot.management.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/log") +public class LoggingController { + private Log log = LogFactory.getLog(LoggingController.class); + + @GetMapping + public String log() { + log.trace("This is a TRACE level message"); + log.debug("This is a DEBUG level message"); + log.info("This is an INFO level message"); + log.warn("This is a WARN level message"); + log.error("This is an ERROR level message"); + + return "See the log for details"; + } +} diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/SecurityConfig.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/SecurityConfig.java new file mode 100644 index 0000000000..45cc1ebb33 --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/logging/SecurityConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.boot.management.logging; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf() + .ignoringAntMatchers("/actuator/**"); + } +} diff --git a/spring-boot-management/src/main/resources/application.properties b/spring-boot-management/src/main/resources/application.properties index 835ca64eac..338251cf9b 100644 --- a/spring-boot-management/src/main/resources/application.properties +++ b/spring-boot-management/src/main/resources/application.properties @@ -1,3 +1,22 @@ -management.endpoints.web.exposure.include=httptrace +management.endpoints.web.exposure.include=httptrace,loggers,health,info,metrics management.trace.http.include=RESPONSE_HEADERS +management.endpoint.loggers.enabled=true + +#basic auth creddentials +spring.security.user.name=client +spring.security.user.password=client + +#configs to connect to a secured server +spring.boot.admin.client.url=http://localhost:8080 +spring.boot.admin.client.username=admin +spring.boot.admin.client.password=admin + +#configs to give secured server info +spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name} +spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password} + +#app config +spring.application.name=spring-boot-management +server.port=8081 + diff --git a/spring-boot-management/src/main/resources/logback.xml b/spring-boot-management/src/main/resources/logback.xml new file mode 100644 index 0000000000..36314cbeeb --- /dev/null +++ b/spring-boot-management/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + \ No newline at end of file