做网站界面用的软件,网站建设样式,html网站怎么做的,网站如何做rss订阅1. 介绍
Spring Boot Actuator 是一个用于监控和管理 Spring Boot 应用程序的功能模块。它提供了一系列生产就绪的功能#xff0c;帮助你了解应用程序的运行状况#xff0c;以及在运行时对应用程序进行调整。Actuator 使用了 Spring MVC 来暴露各种 HTTP 或 JMX 端点#x…
1. 介绍
Spring Boot Actuator 是一个用于监控和管理 Spring Boot 应用程序的功能模块。它提供了一系列生产就绪的功能帮助你了解应用程序的运行状况以及在运行时对应用程序进行调整。Actuator 使用了 Spring MVC 来暴露各种 HTTP 或 JMX 端点通过这些端点你可以获取到应用程序的运行信息如健康状态、指标、线程 dump、环境变量等。 Spring Boot Actuator 的主要特性包括
健康检查可以检查应用程序的运行状况包括数据库连接、磁盘空间、服务状态等。指标收集收集应用程序的性能指标如内存使用情况、处理器使用情况、HTTP 请求计数等。HTTP 端点暴露了一系列的 HTTP 端点通过这些端点可以访问应用程序的运行信息。日志管理可以动态地修改应用程序的日志级别。跟踪和应用信息提供了对应用程序的跟踪信息和应用信息的访问。线程转储可以获取应用程序的线程转储信息帮助诊断性能问题。环境信息可以查看应用程序的配置属性和环境变量。映射信息可以查看 Spring MVC 的映射信息。审计事件可以访问应用程序的审计事件信息。 要启用 Spring Boot Actuator你需要在项目中包含相应的依赖然后在配置文件中配置相关的属性。Spring Boot 2.x 版本中Actuator 的默认端点是通过 HTTP 公开的但是出于安全考虑除了 /health 和 /info 端点之外其他端点默认是不对外暴露的。你可以通过配置文件来开启这些端点并设置是否需要认证访问。 Spring Boot Actuator 是开发和管理生产级 Spring Boot 应用程序的重要工具它可以帮助你确保应用程序的稳定性和性能。
2. 问题描述
!-- SpringBoot Actuator --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency当我们的项目只是引入了 actuator 模块时默认只公开了几个接口如 访问http://localhost:9200/actuator 有些情况下我们需要将服务的健康信息上报给安全监控服务则需要将接口打开
# 暴露监控端点
management:endpoints:web:exposure:include: *此时再次访问http://localhost:9200/actuator 也可以访问具体的属性http://localhost:9200/actuator/env 我们发现这个时候就会暴露很多服务信息安全性得不到保证。
3. 解决方案
3.1 springboot1.x
3.1.1 禁用所有端口
#关闭全部接口
endpoints.enabled false###只开启某些接口
#endpoints.beans.enabled true
#endpoints.env.enabled true
#endpoints.trace.enabled true
#endpoints.metrics.enabled true3.1.2 安全框架控制
引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId
/dependency配置输入账号密码验证后才允许访问
management.security.enabledtrue
security.user.nameadmin
security.user.passwordadmin3.2 springboot2.x
3.2.1 禁用所有端口
management.endpoints.enabled-by-default: false3.2.2 安全框架控制
引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId
/dependency配置输入账号密码验证后才允许访问
spring.security.user.nameactuator
spring.security.user.passwordactuator添加配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/*** Actuator 监控端点权限**/
Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
Override
protected void configure(HttpSecurity http) throws Exception {http.httpBasic().and().authorizeRequests().antMatchers(/actuator/**).authenticated().anyRequest().permitAll();http
// 关闭csrf token认证不需要csrf防护.csrf().disable()
// 关闭Session会话管理器 JWT 不需要.sessionManagement().disable()
// 关闭记住我功能 JWT 不需要.rememberMe().disable();}
}4. 参考资料
https://zhuanlan.zhihu.com/p/602691208 https://springdoc.cn/spring-boot-actuator-enable-endpoints/ https://blog.csdn.net/weixin_44106034/article/details/133934404 https://developer.aliyun.com/article/1079170