当前位置: 首页 > news >正文

SpringBoot Actuator未授权访问漏洞的解决方法

在这里插入图片描述

1. 介绍

Spring Boot Actuator 是一个用于监控和管理 Spring Boot 应用程序的功能模块。它提供了一系列生产就绪的功能,帮助你了解应用程序的运行状况,以及在运行时对应用程序进行调整。Actuator 使用了 Spring MVC 来暴露各种 HTTP 或 JMX 端点,通过这些端点你可以获取到应用程序的运行信息,如健康状态、指标、线程 dump、环境变量等。
Spring Boot Actuator 的主要特性包括:

  1. 健康检查:可以检查应用程序的运行状况,包括数据库连接、磁盘空间、服务状态等。
  2. 指标收集:收集应用程序的性能指标,如内存使用情况、处理器使用情况、HTTP 请求计数等。
  3. HTTP 端点:暴露了一系列的 HTTP 端点,通过这些端点可以访问应用程序的运行信息。
  4. 日志管理:可以动态地修改应用程序的日志级别。
  5. 跟踪和应用信息:提供了对应用程序的跟踪信息和应用信息的访问。
  6. 线程转储:可以获取应用程序的线程转储信息,帮助诊断性能问题。
  7. 环境信息:可以查看应用程序的配置属性和环境变量。
  8. 映射信息:可以查看 Spring MVC 的映射信息。
  9. 审计事件:可以访问应用程序的审计事件信息。
    要启用 Spring Boot Actuator,你需要在项目中包含相应的依赖,然后在配置文件中配置相关的属性。Spring Boot 2.x 版本中,Actuator 的默认端点是通过 HTTP 公开的,但是出于安全考虑,除了 /health/info 端点之外,其他端点默认是不对外暴露的。你可以通过配置文件来开启这些端点,并设置是否需要认证访问。
    Spring Boot Actuator 是开发和管理生产级 Spring Boot 应用程序的重要工具,它可以帮助你确保应用程序的稳定性和性能。

2. 问题描述

<!-- SpringBoot Actuator -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-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 = true
3.1.2 安全框架控制

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置输入账号密码验证后才允许访问

management.security.enabled=true
security.user.name=admin
security.user.password=admin

3.2 springboot2.x

3.2.1 禁用所有端口
management.endpoints.enabled-by-default: false
3.2.2 安全框架控制

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置输入账号密码验证后才允许访问

spring.security.user.name=actuator
spring.security.user.password=actuator

添加配置类

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

http://www.lryc.cn/news/344007.html

相关文章:

  • AI大模型探索之路-训练篇18:大语言模型预训练-微调技术之Prompt Tuning
  • Ollamallama
  • 苹果Mac用户下载VS Code(Universal、Intel Chip、Apple Silicon)哪个版本?
  • Linux(Ubuntu)安装CGAL(非root)
  • hadoop学习---基于Hive的教育平台数据仓库分析案例(三)
  • RAFT:引领 Llama 在 RAG 中发展
  • 上海亚商投顾:沪指缩量调整 合成生物概念股持续爆发
  • Maven+Junit5 + Allure +Jenkins 搭建 UI 自动化测试实战
  • docker学习笔记(三)搭建NFS服务实验
  • super关键字
  • 【经典算法】LeetCode 200. 岛屿数量(Java/C/Python3/Go实现含注释说明,中等)
  • Hive SQL-DQL-Select查询语句用法详解
  • 沙盘Sandboxie v5.56.4
  • Arcpy开发记录
  • Android使用itextpdf操作PDF文档
  • llama_index微调BGE模型
  • 什么是限流?常见的限流算法
  • ZL-0895小动物活动记录仪可同时检测8只动物的活动量
  • 注册测绘师的前世今生
  • Python中的异常处理:深入探索try-except-finally结构
  • 【R语言】边缘概率密度图
  • 中国结(科普)
  • 使用Android Studio 搭建AOSP FrameWork 源码阅读开发环境
  • 区块链 | IPFS:CID
  • PostgreSQL(十二)报错:Tried to send an out-of-range integer as a 2-byte value: 51000
  • Linux守护进程
  • HarmonyOS 应用开发——入门
  • 开源免费的发票识别OCR应用:Invoice
  • 关于Docker alpine
  • 【Elasticsearch运维系列】Elasticsearch7.12.1启动指定版本JDK:你学废了吗?