SpringBoot 2.2.5 整合SpringBoot Admin监控管理平台,并配置安全认证机制

说明

codecentric的Spring Boot Admin是一个社区项目,用于管理和监视您的Spring Boot®应用程序。这些应用程序在我们的Spring Boot Admin Client中注册(通过HTTP),或者是通过Spring Cloud®(例如Eureka,Consul)发现的。 UI只是Spring Boot Actuator端点之上的Vue.js应用程序。

功能

Spring Boot Admin提供了很多服务治理方面的功能,利用它能节省我们很多在治理服务方面的时间和精力Spring Boot Admin提供了如下功能(包括但不限于):

  1. 显示健康状态及详细信息,如JVM和内存指标、数据源指标、缓存指标
  2. 跟踪并下载日志文件
  3. 查看jvm系统-和环境属性
  4. 查看Spring启动配置属性
  5. 方便loglevel管理
  6. 查看线程转储
  7. 视图http-traces
  8. 查看http端点
  9. 查看计划任务
  10. 查看和删除活动会话(使用spring-session)
  11. 状态更改通知(通过电子邮件、Slack、Hipchat…)
  12. 状态变化的事件日志(非持久性)
  13. ……(and more !)

完整代码地址在结尾!!

第一步,创建admin-server工程,作为admin server端,在pom.xml加入依赖,如下

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

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

第二步,编写application.yml配置文件,如下

server:
  port: 8184
  servlet:
    context-path: /monitor # 根路径

spring:
  application:
    name: admin-server
  security:
    user:
      name: root # 安全认证账号
      password: root # 安全认证密码

第三步,创建DimplesSecurityConfigure配置文件,配置相关路径,如下

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

@EnableWebSecurity
public class DimplesSecurityConfigure extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public DimplesSecurityConfigure(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(adminContextPath + "/login").permitAll()
                // 放开client注册路径
                .antMatchers("/actuator/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf().disable();
    }

}

解释

  1. 配置免认证路径,比如/assets/**静态资源和/login登录页面;配置了登录页面为/login,登出页面为/logout
  2. 在Admin中是通过spring-boot-starter-actuator提供的/actuator/监控接口来实现的,所以我们需要将/actuator/资源纳入到免认证路径中

第四步,在admin-server工程启动类加上@EnableAdminServer注解,如下

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(AdminServerApplication.class, args);
	}

}

然后启动admin-server工程,访问http://localhost:8184/monitor,账号密码为上面配置文件里面的内容
登录进去后可以看到暂时还没有应用注册进来,下面创建应用并注册进去

第五步,创建admin-client工程,作为admin client端,在pom.xml加入依赖,如下

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

第六步,编写application.yml配置文件,如下

server:
  port: 8185

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8184/monitor # admin server地址
        username: root # admin server安全认证账号
        password: root # admin server安全认证密码

management:
  endpoints:
    web:
      exposure:
        include: "*" # 打开actuator的监控

# 额外显示信息
info:
  app:
    name: "@project.name@"
    description: "@project.description@"
    version: "@project.version@"

第七步,启动admin-client工程

此时回到http://localhost:8184/monitor页面,可以看到admin-client已经显示在里面,还能查到各种应用的详情信息

完整代码地址:https://github.com/Jinhx128/springboot-demo

注:此工程包含多个module,本文所用代码均在admin-demo模块下

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值