自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBootAdmin:輕量級的SpringBoot監(jiān)控組件,用過的都說好

開發(fā) 架構(gòu)
Springboot Admin是一個管理和監(jiān)控Springboot項(xiàng)目的組件,分為服務(wù)端和客戶端,兩端通過http進(jìn)行通信。由于其輕量級的特性,所以特別適合中小項(xiàng)目使用。

簡介

Springboot Admin是一個管理和監(jiān)控Springboot項(xiàng)目的組件,分為服務(wù)端和客戶端,兩端通過http進(jìn)行通信。由于其輕量級的特性,所以特別適合中小項(xiàng)目使用。

其效果圖如下:

服務(wù)端配置

1、引入Springboot admin和Spring Security依賴。

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、配置相關(guān)屬性。

server:
port: 8080
servlet:
context-path: /server
spring:
security:
user:
#admin Server端登錄時用的賬戶密碼
name: server123
password: 123456
boot:
admin:
instance-auth:
#啟用header驗(yàn)證
enabled: true
#Server訪問client接口時會使用下面的配置生成authorization
default-user-name: "name_shishan"
default-password: "pwd_shishan"

3、配置@EnableAdminServer注解。

@SpringBootApplication
@Configuration
@EnableAdminServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}

經(jīng)過以上3步,服務(wù)端就可以啟動了。

訪問??http://localhost:8080/server/,就可以看到以下登錄界面。??

使用在yml文件中配置的賬戶密碼就可以登錄了。

客戶端配置

1、在我們要監(jiān)控的客戶端中加入以下依賴。

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

2、暴露監(jiān)控接口以及配置Server地址。

客戶端在啟動后會向配置的Server發(fā)起注冊申請,此時為了安全性還需要Server端的賬戶密碼進(jìn)行校驗(yàn)。

spring:
boot:
admin:
client:
#admin注冊地址
url: http://localhost:8080/server
#配置admin的賬戶
username: server123
password: 123456
admin:
header:
auth:
name: "name_shishan"
password: "pwd_shishan"
#暴露出端口
management:
endpoints:
web:
exposure:
include: "*"

3、對暴露的接口進(jìn)行權(quán)限校驗(yàn)。

由于我們將監(jiān)控接口進(jìn)行了暴露,所以必須對相關(guān)的接口進(jìn)行權(quán)限校驗(yàn),否則就有可能泄露相關(guān)信息。

對接口進(jìn)行權(quán)限過濾有很多種選擇,比如設(shè)置IP訪問的白名單,只允許admin Server所在的服務(wù)器訪問,也可以配置相關(guān)的token等等。

下面我們以一個簡單的接口過濾器實(shí)現(xiàn)對/actuator/**相關(guān)接口的權(quán)限校驗(yàn)。

@Component
public class PathFilter implements Filter {

@Value("${admin.header.auth.name}")
private String username;
@Value("${admin.header.auth.password}")
private String password;

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
AntPathMatcher antPathMatcher = new AntPathMatcher();
if (antPathMatcher.match("/actuator/**", request.getServletPath())) {
String authorization = request.getHeader("authorization");
if (StringUtils.hasText(authorization)) {
String token = Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
if (authorization.equals("Basic " + token)) {
//token匹配才放行
filterChain.doFilter(request, servletResponse);
return;
}
}
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().print("權(quán)限不足");
return;
}
//其他接口直接放行
filterChain.doFilter(request, servletResponse);
}
}

在這個filter中,對actuator相關(guān)的接口進(jìn)行了header參數(shù)的校驗(yàn),只有通過校驗(yàn)才可以訪問暴露出的actuator接口。

當(dāng)然,如果我們使用了SpringSecurity或者SaToken這樣的第三方權(quán)限框架,也可以去重寫相關(guān)的配置完成權(quán)限的判斷,原理都是一樣的。

下面我們看一下最終的監(jiān)控效果:

最后

除了通過普通http請求方式獲取監(jiān)控信息以外,Springboot admin還支持通過注冊中心的方式獲取相關(guān)信息,在其官方文檔大家也可以看到相關(guān)的配置。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2009-07-14 18:05:28

輕量級Swing組件

2009-07-17 14:38:51

輕量級Swing組件

2025-01-15 08:56:53

2023-02-24 16:33:37

VS Code插件

2023-09-27 00:12:23

2022-07-15 16:39:19

PythonWhoosh工具

2021-04-14 13:32:50

Redis輕量級分布式

2023-06-27 16:42:18

Tinygrad深度學(xué)習(xí)工具

2022-12-29 09:49:06

輕量級架構(gòu)決策

2022-03-23 08:01:04

Python語言代碼

2014-06-11 20:46:51

Monitorix監(jiān)控系統(tǒng)

2020-06-19 15:38:08

分析工具GoatCounter開發(fā)

2025-01-03 09:17:26

JavaSpringBoot

2009-01-19 09:28:42

JSONJavaScriptJSON結(jié)構(gòu)

2019-12-13 19:00:26

PekwmLinux桌面

2020-03-02 19:08:21

JVMJDKJRE

2023-08-09 08:01:38

場景Redis接口

2009-09-11 08:26:49

Linux系統(tǒng)CRUX 2.6Linux

2009-03-09 21:06:35

LinuxEvilvte終端模擬器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號