Spring Boot Admin

2019/8/7 posted in  SpringBoot

Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者使用注册中心注册到 Admin server中进行展示,Spring Boot Admin UI 部分使用 AngularJs 将数据展示在前端。
Spring Boot Admin 是一个针对spring-boot 的 actuator 接口进行UI美化封装的监控工具。他可以:在列表中浏览所有被监控 spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。

Spring Boot Admin 服务端安装

如果我们使用的是单个 Spring Boot应用,就需要在每一个被监控的应用中配置 Admin Server的地址信息;如果应用都注册在 注册中心(Nacos) 中就不需要再对每个应用进行配置,Spring Boot Admin会自动从注册中心抓取应用的相关信息。
此教程默认已经配置了nacos注册中心
在pom.xml添加如下代码:

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.3</version>
        </dependency>

在Application文件中加入注解

@EnableAdminServer

Spring Boot Admin 客户端安装

在pom.xml添加如下代码:

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.3</version>
        </dependency>

在bootstrap.yml文件中加入:

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

如果要使用 SpringBoot Admin 的 Loggin Logfile动态Console功能,需要配置日志文件的输出地址,比如在yml文件加入如下:

logging:
  level:
    root: info
  file: logs/sts.log

SpringBoot Admin 安全登录

默认的情况下,SpringBoot Admin 的服务地址可以直接访问,并不需要验证用户,所以在此,我们可以用 SpringSecurity 集成来验证用户,在Spring Boot Admin 服务端的pom.xml加入如下代码:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

然后在bootstrap.yml文件中加入:

spring:
  # 安全配置
  security:
    user:
      name: admin
      password: admin

添加一个配置类:

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
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;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dyk
 * Create time: 2019-08-02  11:35
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter( "redirectTo" );

        http.authorizeRequests()
                .antMatchers( adminContextPath + "/assets/**" ).permitAll()
                .antMatchers( "/actuator/**" ).permitAll()
                .antMatchers( adminContextPath + "/login" ).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
                .logout().logoutUrl( adminContextPath + "/logout" ).and()
                .httpBasic().and()
                .csrf().disable();
    }
}

现在进入 SpringBoot Admin服务端 就需要输入用户名和密码为 admin 才能进入控制台