SpringBoot Admin的简单使用的方法步骤

网友投稿 354 2022-09-03


SpringBoot Admin的简单使用的方法步骤

目录一、快速入门1.1 SpringBoot Admin服务端的搭建1.2 SpringBootAdmin client端搭建1.3 效果展示二、安全性2.1 admin-server端安全加固2.2 admin-client端的安全三、小结

公司有个SpringBoot项目需要加个监控,网上找了下发现大家都在推荐SpringBootAdmin。SpringBoot Admin是开源社区孵化的项目,用于对SpringBoot应用的管理和监控。SpringBoot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用http通讯方式实现数据交互;单体项目中需要整合spring-boot-admin-client才能让应用被监控。在SpringCloud项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服务应用整合spring-boot-admin-client就可以实现应用的管理和监控。

官网参考链接:https://codecentric.github.io/spring-boot-admin/2.2.4/

本文只叙述SpringBoot Admin 管理和监控单体应用 ,不涉及SpringCloud相关的内容 。

一、快速入门

1.1 SpringBoot Admin服务端的搭建

(1) Maven依赖说明 SpringBoot版本

org.springframework.boot

spring-boot-starter-parent

2.2.10.RELEASE

添加SpringBootAdmin server依赖及SpringBoot web 依赖

org.springframework.boot

spring-boot-starter-web

de.codecentric

spring-boot-admin-starter-server

2.2.4

(2)application.yml中配置端口

# 指定端口

server:

port: 23333

(3)编写启动类并开启SpringBootAdminServer

package com.zcode.monitor.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* AdminServerApplication

* @author ZENG.XIAO.YAN

* @version 1.0

* @Date 2020-11-12

*/

@EnableAdminServer // 开启 springboot admin 服务端

@SpringBootApplication

public class AdminServerApplication {

public static void main(String[] args) {

SpringApplication.run(AdminServerApplication.class,args);

}

}

(4)浏览器访问测试 浏览器访问 http://localhost:23333/ 出现以下页面说明SpringBoot Admin服务端搭建成功

1.2 SpringBootAdmin client端搭建

备注:所谓的 client端就是指我们需要被监控的应用端。这里我们写一个简单点的SpringBoot web应用做演示

(1)Maven依赖说明

SpringBoot版本如下

org.springframework.boot

spring-boot-starter-parent

2.2.10.RELEASE

添加SpringBootAdmin client 依赖及SpringBoot web 依赖。这里不需要添加SpringBoot actuator 依赖,因为SpringBootAdmin client里面已经包含了actuator相关依赖

org.springframework.boot

spring-boot-starter-web

de.codecentric

spring-boot-admin-starter-client

2.2.4

(2) application.yml配置

在yml中需要 配置如下信息:

应用端口

开放端点用于SpringBootAdmin 监控

配置应用名称(该名称会在SpringBoot Admin的管理页面显示)

配置Admin Server的地址

配置下日志文件的文件名和存放位置 (如果不配置则会看不到日志)

# 端口

server:

port: 9088

#开放端点用于SpringBoot Admin的监控

management:

endpoints:

web:

exposure:

include: '*'

spring:

application:

name: admin-client # 给client应用取个名字

boot:

admin:

client:

url: http://localhost:23333 #这里配置admin server 的地址

logging:

file:

name: admin-client.log #配置生成日志文件名称

(3)写一个Controller模拟一个普通的接口

通过浏览器访问这个接口就会打印日志,具体代码如下

/**

* HelloController

*

* @author ZENG.XIAO.YAN

* @version 1.0

* @Date 2020-11-16

*/

@Slf4j

@RestController

@RequestMapping("api")

public class HelloController {

private AtomicInteger count = new AtomicInteger(0);

@GetMapping("hi")

private String sayHi() {

// 每次进来如打印下日志

log.info("{} 啪...我第{}次进来了.", LocalDateTime.now(), count.addAndGet(1));

// 每次进来new 个大对象,便于监控观察堆内存变化

byte[] bytes = new byte[100*1024*1024];

log.info("new了 100MB");

return "hi springboot addmin " + LocalDateTime.now();

}

}

(4)写个启动类

启动类代码就很简单了,就是一个普通的SpringBoot项目的启动类,上面没加其他注解。具体如下

@SpringBootApplication

public class AdminClientApplication {

public static void main(String[] args) {

SpringApplication.run(AdminClientApplication.class, args);

}

}

1.3 效果展示

(1)已管理的应用会在应用墙上展示

当我们的admin-client项目启动后,在 admin-server的管理页面的应用墙上就能看到admin-client这个应用了,具体可参考下图

(2)可查看应用的具体信息

在应用墙点击这个应用,我们可以看到这个应用的具体信息,如堆内存变化及线程数等。具体可参考下图

(3)日志查看及堆内存变化观察

请求我们在admin-client中写的模拟接口 http://localhost:9088/api/hi ,该接口请求一次则会输出日志,同时开辟100MB的堆内存空间。请求多次后在网页上可以实时的看到日志如下图

由于我们直接new了100MB的大对象,此时可以查看cqyGv细节中的堆内存变化;具体如下图

二、安全性

2.1 admin-server端安全加固

这个SpringBoot Admin的管理后台如果没密码就能访问,那实在太不安全了,因此我们要给它加上登录的功能。

参考SpringBoot Admin的官方文档,我们可以在Admin-Server端添加Spring Security 相关依赖及就可以实现需要登录后才能访问网页管理面板。

官网参考链接为:https://codecentric.github.io/spring-boot-admin/2.2.4/#_securing_client_actuator_endpoints

下面开始具体的改造

(1)admin-server添加Spring Security 相关依赖

org.springframework.boot

spring-boot-starter-security

(2)admin-server 设置账号和密码

在application.yml配置账号和密码

# 配置一个账号和密码

spring:

security:

user:

name: admin

password: root123456

(3)admin-server 添加一个Spring Security 配置类

@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");

successHandler.setDefaultTargetUrl(adminContextPath + "/");

http.authorizeRequests()

//1.配置所有静态资源和登录页可以公开访问

.antMatchers(adminContextPath + "/assets/**").permitAll()

.antMatchers(adminContextPath + "/login").permitAll()

.anyRequest().authenticated()

.and()

//2.配置登录和登出路径

.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()

.logout().logoutUrl(adminContextPath + "/logout").and()

//3.开启http basic支持,admin-client注册时需要使用

.httpBasic().and()

.csrf()

//4.开启基于cookie的csrf保护

.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())

//5.忽略这些路径的csrf保护以便admin-client注册

.ignoringAntMatchers(

adminContextPath + "/instances",

adminContextPath + "/actuator/**"

);

}

}

(4)admin-server 安全加固后访问测试

再次访问http://localhost:23333/ ,发现需要登录

当我们输入正确的账号密码登录后,情况如下图

这个时候的应用数居然变成了0了,在我们没进行安全加固时是有一个admin-client应用的,为什么就不见了? 原因是添加了账号密码认证后,admin-client端也需要配置下 admin-server的账号和密码。

(5)admin-client 端设置 admin-server的账号密码

admin-client 注册到 admin-server时,admin-server端有个http Basic认证,通过了认证后 admin-client才能注册到 admin-server上。 admin-client的application.yml中配置访问密码配置可参考下面代码

spring:

application:

name: admin-client # 给client应用取个名字

boot:

admin:

client:

url: http://localhost:23333 #这里配置admin server 的地址

# 配置 admin-server的账号和密码

username: admin

password: root123456

(6) 再次访问 admin-server 管理后台 当我们登录后,终于再次看到了我们的admin-client这个应用

2.2 admin-client端的安全

admin-client端如果把actuator端点都暴露出来,是非常不安全的。因此我们可以添加Spring Security对admin-client 也进行安全加固。

下面所有操作均在admin-client中进行

(1)添加Spring Security依赖

org.springframework.boot

spring-boot-starter-security

(2)yml配置

yml中需要设置client的账号和密码,官网相关配置如下图所示

本次演示的admin-client的相关yml配置参考下面代码

spring:

application:

name: admin-client # 给client应用取个名字

boot:

admin:

client:

url: http://localhost:23333 #这里配置admin server 的地址

# 配置 admin-server的账号和密码

username: admin

password: root123456

instance:

metadata:

# 这里配置admin-client的账号和密码

user.name: ${spring.security.user.name}

user.password: ${spring.security.user.password}

# admin-client 的用户名和密码

security:

user:

name: clientAdmin

password: 123456

(3)添加Spring Security 配置类

为何要到配置?因为Spring Security不配置时会把所有请求都拦截的,而我们这里只需要拦截监控端点/actuator/**即可。同时,官网中提到admin-server访问admin-client时,也是采用http Basic认证方式的;因此需要配置Spring Security支持Http Basic认证方式。

@Configuration

@Slf4j

public class SpringSecurityActuatorConfig extends WebSecurityConfigurerAdapter {

public SpringSecurityActuatorConfig() {

log.info("SpringSecurityActuatorConfig... start");

}

@Override

protected void configure(HttpSecurity http) throws Exception {

// 这个配置只针对 /actuator/** 的请求生效

http.antMatcher("/actuator/**")

// /actuator/下所有请求都要认证

.authorizeRequests().anyRequest().authenticated()

// 启用httpBasic认证模式,当springboot admin-client 配置了密码时,

// admin-server走httpbasic的认证方式来拉取client的信息

.and().httpBasic()

// 禁用csrf

.and().csrf().disable();

}

}

(4)效果展示

admin-server端依旧能看到admin-client的信息,说明我们添加SpringSecurity 后 admin-server的监控管理功能正常,具体见下图

当我们去访问admin-client的监控端点http://localhost:9088/actuator/health 时,发现需要进行http Basic认证;这也证明了我们的认证拦截只拦截了监控端点。效果如下图

(5)存在的问题

通过上面的一通配置,admin-client 添加 Spring Security 对actuator的端点进行安全认证的功能是实现了,但也存在着问题。 当我们项目本来就是使用SpringSecurity 安全框架进行认证和授权时。上述的配置就要做修改了。因为我们一般都不用HttpBasic认证,而是用的表单登录认证。也就出现了配置多个Spring Security的问题。虽然有这个问题,但是网上还是有解决方案的。

(6)多个Spring Security共存方案

这个方案是在Spring Security官方文档里面找到的 链接为: https://docs.spring.io/spring-security/site/docs/5.3.5.RELEASE/reference/html5/ 官网关键信息截图如下:

里面的重点就是通过添加Order注解来指定多个Spring Security的优先级

下面直接贴上我的代码;为了直观,我就在同一个类里面建了2个静态的Spring Security配置类

/**

* SpringSecurity 表单和HttpBasic 共存配置参考,写在一个类里面方便对比

* @author ZENG.XIAO.YAN

* @Date 2020-11-11

* @version 1.0

*/

@Slf4j

public class SpringSecurityConfig2 {

/*

* 这个表单和HttpBasic 共存配置玩法,参考url如下:

* 官方url:https://docs.spring.io/spring-security/site/docs/4.2.3.BUILD-SNAPSHOT/reference/htmlsingle/#multiple-httpsecurity

* 项目启动日志如下,可以看到创建了2条过滤链

* 2020-11-11 22:57:56.340 INFO 12692 --- [main] o.s.s.web.DefaultSecurityFilterChain: Creating filter chain: Ant [pattern='/actuator/**'],

* 2020-11-11 22:57:56.344 INFO 12692 --- [main] o.s.s.web.DefaultSecurityFilterChain: Creating filter chain: any request,

*/

/**

* HttpBasic 认证方式,只对/actuator/** 生效,由于设置了Order,优先级会高于FormLoginWebSecurityConfigurerAdapter

* @author ZENG.XIAO.YAN

* @Date 2020-11-11

* @version 1.0

*/

@Configuration

@Order(1)

public static class HttpBasicSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

public HttpBasicSecurityConfigurationAdapter() {

log.info("HttpBasicSecurityConfigurationAdapter... start");

}

protected void configure(HttpSecurity http) throws Exception {

// 这个配置只针对 /actuator/** 的请求生效

http.antMatcher("/actuator/**")

// /actuator/下所有请求都要认证

.authorizeRequests().anyRequest().authenticated()

// 启用httpBasic认证模式,当springboot admin-client 配置了密码时,

// admin-server走httpbasic的认证方式来拉取client的信息

.and().httpBasic()

// 禁用csrf

.and().csrf().disable();

}

}

/**

* 表单登录认证方式配置,由于没有指定Order,所以默认是最大2147483647,数值越大,优先级越低

* @author ZENG.XIAO.YAN

* @Date 2020-11-11

* @version 1.0

*/

@Configuration

public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

public FormLoginWebSecurityConfigurerAdapter() {

log.info("FormLoginWebSecurityConfigurerAdapter... start");

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.anyRequest().authenticated()

.and()

.formLogin();

}

}

}

添加完这个配置类后,记得把我们上面配置的SpringSecurityActuatorConfig 这个类删除了,然后重启项目。效果如下:

访问http://localhost:9088/actuator/health ,则出现的是httpBasic认证的页面

访问 http://localhost:9088/api/hi,则出现的是Spring Security 自带的表单登录页面

访问admin-server 的管理页面,发现admin-client应用信息正常,说明本次修改的Spring Security配置没有问题

三、小结

(1)本文介绍了SpringBoot Admin的简单使用,同时cqyGv介绍了admin-server端的安全配置和admin-client端的安全配置

(2)在介绍admin-client端的安全配置时,引申出了 如何实现多个SpringSecurity 配置 共存


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Could not fetch URL https://pypi.org/simple/tensorboard/:There was a problem confirming the ssl
下一篇:ProxyError: Conda cannot proceed due to an error in your proxy configuration(proxyerror什么意思)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~