以下为 Jean 学习笔记和习题整理,供学习和参考
# 介绍
区分两个概念:
服务端:Spring Boot Admin 作为 Server,作为监控作用。
客户端:其他 Spring Boot 应用作为 Client,Client 把自身的信息 “注册” 到 Server,我们就能在 Server 上看到 “注册” 的 Spring Boot 应用的状态信息了。
# 1. 服务端搭建:
# pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<!--Spring Admin--> | |
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server --> | |
<dependency> | |
<groupId>de.codecentric</groupId> | |
<artifactId>spring-boot-admin-starter-server</artifactId> | |
<version>2.5.0</version> | |
</dependency> |
# 主函数入口:
注意一定要有 @EnableAdminServer
/** | |
* @author Jean | |
* @date 2022/7/4 | |
* @description : admin-Server | |
*/ | |
@EnableAdminServer | |
@SpringBootApplication | |
public class AdminServiceApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(AdminServiceApplication.class, args); | |
} | |
} |
# yml:
指定端口
# 端口 | |
server: | |
port: 9088 |
至此服务端可以访问,但是没有实例。
# 2. 客户端搭建:
# pom:
我的 SpringBoot 的版本是 2.5.14
<!-- SpringBoot Admin Client --> | |
<dependency> | |
<groupId>de.codecentric</groupId> | |
<artifactId>spring-boot-admin-starter-client</artifactId> | |
<version>2.5.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-actuator</artifactId> | |
</dependency> |
yml:
# 端口
server:
port: 8060
#开放端点用于SpringBoot Admin的监控
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
spring:
application:
name: admin-client # 给client应用取个名字
boot:
admin:
client:
url: http://localhost:9088 #这里配置admin server 的地址
instance:
service-url: http://localhost:8060/
logging:
file:
name: admin-client.log #配置生成日志文件名称
至此可以在客户端看见注册的实例
# 安全性
# admin-server 安全加固:
在 pom 中:添加安全依赖
<!--springboot admin 安全相关--> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
</dependency> |
在 yml 文件中:
sping: | |
security: | |
user: | |
name: admin | |
password: root123456 |
加入如下配置:
@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/**" | |
); | |
} | |
} |
# 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 |