第一部分 Spring Boot Admin 簡介
- Spring Boot Admin用來管理和監控Spring Boot應用程式,
- 應用程式向我們的Spring Boot Admin Client注冊(通過HTTP)或使用SpringCloud?(例如Eureka,Consul)發現,
- UI是Spring Boot Actuator端點上的Vue.js應用程式,
Spring Boot Admin 是一個管理和監控Spring Boot 應用程式的開源軟體,每個應用都認為是一個客戶端,通過HTTP或者使用 Eureka注冊到admin server中進行展示,Spring Boot Admin UI部分使用AngularJs將資料展示在前端,
Spring Boot Admin 是一個針對spring-boot的actuator介面進行UI美化封裝的監控工具,他可以:在串列中瀏覽所有被監控spring-boot專案的基本資訊,詳細的Health資訊、記憶體資訊、JVM資訊、垃圾回收資訊、各種配置資訊(比如資料源、快取串列和命中率)等,還可以直接修改logger的level,
admin-server 服務端(admin-server)
服務端:是指Spring Boot Admin這個應用(通常就是指監控服務器),一個服務端可以監控多個客戶端,
客戶端
客戶端是:被服務端監控的物件(通常就是指你的業務系統),
第二部分 快速入門
本部分將為您展示SpringBoot ADMIN 的簡單應用,
服務端配置(admin-server)
步驟一:搭建springboot maven專案
搭建一個基于SpringBoot的專案,注意您所使用的SpringBoot版本,
步驟二:配置pom.xml檔案
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency>
步驟三:application.properties中配置埠號
此埠號指的是你所搭建的服務器所使用的的版本號,如果服務端和客戶端在同一臺機器上,注意埠號的設定,以防埠出現沖突的情況,
server.port=8099
步驟四:主啟動類上加注解@EnableAdminServer
@SpringBootApplication @EnableAdminServer public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
步驟五:啟動專案
訪問:http://127.0.0.1:8099/applications,監控首頁顯示如下
客戶端配置(admin-client)
步驟一:在客戶端專案(也就是需要監控的springboot專案)中添加jar包
加入Security安全框架的jar包,加入jar需注意版本的問題,有些springboot版本,可能會自動引入失>敗,如圖:
出現這種情況需指定security的版本號,找個適合你springboot版本的security,
具體如下:
<!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.4.1</version> </dependency>
引入 spring-boot-admin-starter-client
<!--admin server 監控--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.4.1</version> </dependency>
步驟二:在啟動組態檔中配置如下 application.properties
#開放端點用于SpringBoot Admin的監控 management.endpoints.web.exposure.include=* # 給client應用取個名字 spring.boot.admin.client.instance.name=zxfdemo #這里配置admin server 的地址 spring.boot.admin.client.url=http://localhost:8099 #這里配置admin client 的地址(客戶端應用程式) spring.boot.admin.client.instance.service-url=http://localhost:8080
步驟四:測驗效果
spring security 安全加固
SpringBoot Admin的管理后臺如果沒密碼就能訪問,那實在太不安全了,所以需要引入一個安全加固的jar包,spring-boot-starter-security
Spring Security是一個功能強大且高度可定制的身份驗證和訪問控制框架,提供了完善的認證機制和方法級的授權功能,是一款非常優秀的權限管理框架,它的核心是一組過濾器鏈,不同的功能經由不同的過濾器,此處就是想通過一個小案例將Spring Security整合到SpringBoot中去,要實作的功能就是在認證服務器上登錄,然后獲取Token,再訪問資源服務器中的資源,
服務端配置(admin-server)
服務端配置修改
1. 服務端添加Spring Security 相關依賴
添加Spring Security 相關依賴
<!-- security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.4.1</version>
</dependency>
2. 服務端設定賬號密碼
spring.security.user.name=zxf spring.security.user.password=123456
3.添加一個Spring Security 配置類
package com.example.springadmintest.config;
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.csrf.CookieCsrfTokenRepository;
/**
* 配置security驗證頁面指向SpringBootAdmin提供的UI界面
*
*
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String contextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.contextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// 跨域設定,SpringBootAdmin客戶端通過instances注冊,見InstancesController
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(contextPath + "/instances");
http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 靜態資源
http.authorizeRequests().anyRequest().authenticated(); // 所有請求必須通過認證
// 整合spring-boot-admin-server-ui
http.formLogin().loginPage("/login").permitAll();
http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
// 啟用basic認證,SpringBootAdmin客戶端使用的是basic認證
http.httpBasic();
}
}
4.登錄頁面展示
再次訪問http://localhost:8099/ ,發現需要登錄

客戶端配置(admin-client)
客戶端配置
1.客戶端添加Spring Security 相關依賴
<!-- security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.4.1</version>
</dependency>
2. 客戶端設定賬號密碼
# 配置 admin-client 地址
spring.boot.admin.client.instance.service-url=http://localhost:8080
#配置 admin-server地址
spring.boot.admin.client.url=http://localhost:8099
# 配置 admin-server的賬號
spring.boot.admin.client.username=zxf
# 配置 admin-server的密碼
spring.boot.admin.client.password=123456
#配置 admin-server的密碼
spring.security.user.name=zxf
#配置 admin-client的密碼
spring.security.user.password=123456
#若在核心組態檔中未添加 management.security.enabled=false 配置,
# 將會導致用戶在訪問部分監控地址時訪問受限,報401未授權錯誤,
management.security.enabled=false
#監控中心配置, 允許監控所有介面
management.endpoints.web.exposure.include=*
3. 客戶端添加Spring Security 配置類
package com.cachedemo.controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
所有配置完成測驗結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283039.html
標籤:其他









