1. What is SBA
The full name of SBA is Spring Boot Admin, which is an open source project for managing and monitoring Spring Boot applications. Divided into two components, admin-server and admin-client, admin-server is a separate microservice, which collects actuator endpoint data and displays it on spring-boot-admin-ui, and the client is our spring boot application. Almost all known endpoints are collected. Through spring-boot-admin, you can dynamically switch the log level, export logs, export heapdump, monitor various indicators, and so on.
Github: https://github.com/codecentric/spring-boot-admin
Official documentation: https://codecentric.github.io/spring-boot-admin/2.3.1/
The spring-boot-admin version used in this article is 2.3.1
2. Build admin-server
2.1. Create a Spring Boot application with the following structure
2.2. Modify pom.xml and introduce spring-boot-admin related dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>devops</groupId>
<version>1.0.0-dev</version>
<artifactId>cherry-boot-admin</artifactId>
<properties>
<spring-boot-admin.version>2.3.1</spring-boot-admin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring-boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-admin server -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<!-- spring-boot-security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3, modify the startup class, add @EnableAdminServer
package com.cherry.boot.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@EnableAdminServer
@SpringBootApplication
@ComponentScan("com.cherry.boot")
public class CherryBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(CherryBootAdminApplication.class, args);
}
}
2.4, configure security
Configure username and password on application.yml
# security
spring:
security:
user:
name: admin
password: 123456
Add a security configuration class
package com.cherry.boot.admin.config;
import java.util.UUID;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
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;
/**
* spring security配置
*
* @author huangqinhua
* @version 1.0.0
* @date 2021/11/04 18:16
*/
@Configuration
public class BootAdminSecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;
public BootAdminSecurityConfig(AdminServerProperties adminServer) {
this.adminServer = adminServer;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
http.authorizeRequests((authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**"))
.permitAll()
.antMatchers(this.adminServer.path("/login"))
.permitAll()
.anyRequest()
.authenticated())
.formLogin((formLogin) -> formLogin.loginPage(this.adminServer.path("/login"))
.successHandler(successHandler)
.and())
.logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
.httpBasic(Customizer.withDefaults())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()),
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))))
.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID()
.toString())
.tokenValiditySeconds(1209600));
}
}
2.5. Start the project, enter http://localhost:8000
Login interface
Main interface