參考博客地址
1、介紹
官網地址
Spring Boot Admin是開源社區范訓的專案,用于對Spring Boot應用的管理和監控,Spring Boot Admin分為服務端(spring-boot-admin-server)和客戶端(spring-boot-admin-client),服務端和客戶端之間采用http通訊方式實作資料互動;單體專案中需要整合spring-boot-admin-client才能讓應用被監控,在SpringCloud專案中,spring-boot-admin-server是直接從注冊中心抓取應用資訊,不需要每個微服務應用整合spring-boot-admin-client就可以實作應用的管理和監控,
2、服務端搭建
2.1 引入依賴
注意:版本要和 Spring Boot 版本對應,例如我的 Spring Boot 是 2.3.7.RELEASE,那么 Spring Boot Admin 對應的版本就是 2.3.x,
<!-- Spring Boot Admin 服務端依賴 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
2.2 添加注解
給啟動類添加一個注解:@EnableAdminServer
@EnableAdminServer
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
2.3 進行測驗
訪問專案的埠號即可!
例如我配置的埠號是 9000,那么直接訪問 http://localhost:9000/ 即可!
2.4 測驗結果

3、客戶端搭建
3.1 引入依賴
注意:版本要和 Spring Boot 版本對應,例如我的 Spring Boot 是 2.3.7.RELEASE,那么 Spring Boot Admin 對應的版本就是 2.3.x,
<!-- Spring Boot Admin 客戶端依賴 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
3.2 撰寫配置
撰寫 application.yml 檔案:
spring:
application:
name: Client
boot:
admin:
client:
# 配置 Admin Server(服務端的名字)
url: http://localhost:9000
server:
port: 9001
# 開放端點用于 SpringBoot Admin 的監控
management:
endpoints:
web:
exposure:
include: '*'
logging:
file:
# 配置生成日志檔案名稱
name: admin-client.log
3.3 進行測驗
啟動專案,然后訪問服務端的 Web 管理界面:


4、安全性
這個
Spring Boot Admin的管理后臺不用賬號密碼就能直接訪問,一點都不安全,因此要給它加上登錄的功能,參考
Spring Boot Admin的官方檔案,我們可以在Admin-Server端添加Spring Security相關依賴及就可以實作需要登錄后才能訪問網頁管理面板,
官網檔案地址
4.1 添加依賴
在服務端添加 Spring Security 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
4.2 撰寫配置
撰寫 application.yml 檔案,撰寫用戶名密碼:
server:
port: 9000
spring:
application:
name: Server
security:
user:
name: admin
password: admin
4.3 撰寫配置類
撰寫 Spring Security 的配置類:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecurityConfig(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 支持,客戶端注冊時需要使用
.httpBasic().and()
.csrf()
// 4. 開啟基于 Cookie 的 CSRF 保護
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
// 5. 忽略這些路徑的 CSRF 保護以便客戶端注冊
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
4.4 修改客戶端配置
修改客戶端的 application.yml 組態檔,添加用戶名密碼:
這里不添加用戶名和密碼的話,是連不上服務端的:
spring:
application:
name: Client
boot:
admin:
client:
# 配置 Admin Server(服務端的名字)
url: http://localhost:9000
# 配置用戶名
username: admin
# 配置密碼
password: admin
4.5 進行測驗
重啟客戶端和服務端專案
訪問效果為:


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352199.html
標籤:java
上一篇:java 相交鏈表
