1、什么是SBA

SBA全称为Spring Boot Admin,是一个管理和监控Spring Boot应用程序的开源项目。分为admin-server 与 admin-client 两个组件,admin-server是一个单独的微服务,通过采集actuator端点数据,显示在spring-boot-admin-ui上,客户端就是我们的spring boot应用。已知的端点几乎都有进行采集,通过 spring-boot-admin可以动态切换日志级别、导出日志、导出heapdump、监控各项指标等等。

Github:https://github.com/codecentric/spring-boot-admin
官方文档:https://codecentric.github.io/spring-boot-admin/2.3.1/

该文章使用的spring-boot-admin版本为2.3.1

2、搭建admin-server

2.1、创建一个Spring Boot应用,结构如下

在这里插入图片描述

2.2、修改pom.xml,引入spring-boot-admin相关依赖
<?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、修改启动类,加上@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、配置security

在application.yml上配置用户名和密码

# security
spring:
  security:
    user:
      name: admin
      password: 123456

新增一个security配置类

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、启动项目,输入http://localhost:8000

登录界面
在这里插入图片描述
主界面
在这里插入图片描述

到此我们服务端的配置就完成了

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐