Spring Boot Actuator基础使用及Actuator未授权访问处理

SpringBoot项目引入Actuator监控

  • pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.7.13</version>
</dependency>
  • bootstrap.yml
management:
  server:
    #管理端口
    port: 8091
  endpoints:
    web:
      exposure:
        #暴露的端点,*表示全部,如果想要开放指定的端点,可以以逗号分割,指定开放,如info,health就表示开放info,health两个端点
        include: '*'
  • 测试
    启动后访问本地actuator端点列表接口 http://127.0.0.1:8091/actuator
    actuator端点列表
    访问一个开放的端点,如health
    health端点.png

代码定位

以health为例,health的端点实现方法为org.springframework.boot.actuate.health.HealthEndpoint#health()

@ReadOperation
public HealthComponent health() {
    HealthComponent health = this.health(ApiVersion.V3, EMPTY_PATH);
    return (HealthComponent)(health != null ? health : DEFAULT_HEALTH);
}

其余端点可以去org.springframework.boot.actuate包下对应端点的包名中寻找响应的Endpoint类即可

自定义端点扩展

  • 新建hello的endpoint
@Component
@Endpoint(id = "hello")
public class HelloEndpoint {

    @ReadOperation
    public String hello() {
        return "hello";
    }
}
  • 运行项目,访问 http://127.0.0.1:8091/actuator
    发现hello端点出现在列表中
    actuator
  • 访问hello端点 http://127.0.0.1:8091/actuator/hello
    自定义端点

添加认证

在实际的项目中,如果未进行特殊处理,actuator往往会触发Actuator未授权访问漏洞,
这种时候需要进行修复,下面推荐几种修复方式

  • 借助security进行授权
    pom中引入security的依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

编写认证代码

@Configuration
@Order(66)
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                // 配置对/actuator/的请求进行拦截
                .authorizeRequests()
                // 仅允许10.9网段及本地访问端点
                .antMatchers("/actuator/**").access("hasIpAddress('10.9.0.0/16') or hasIpAddress('127.0.0.1')")
                .and()
                // 配置一个自定义的访问拒绝处理器(可选,但推荐用于明确的403响应)
                .exceptionHandling().accessDeniedHandler((request, response, accessDeniedException) -> response.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to access!"));
        return http.build();
    }
}

访问 http://127.0.0.1:8091/actuator
,发现可正常返回
127网段访问
访问 http://10.8.0.46:8091/actuator
,返回403
10.8网段

  • 仅开放部分端点
    在部分要求不严格的环境中,可以只开放部分不敏感的端点,如
management:
  server:
    port: 8091
  endpoints:
    web:
      exposure:
        include: health,info
### 复现Spring Boot Actuator未授权访问漏洞 为了展示如何复现Spring Boot Actuator未经授权的访问漏洞,需构建一个简单的Spring Boot应用程序并暴露Actuator端点而无需任何安全措施。 #### 创建无保护的Spring Boot应用 创建一个新的Spring Boot项目,并在`pom.xml`文件中加入依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 接着,在`application.properties`或`application.yml`配置文件里激活所有默认的健康检查和其他管理功能: ```yaml management: endpoints: web: exposure: include: "*" ``` 此时启动该服务,默认情况下所有的敏感操作如查看环境变量(`/env`)、JVM信息(`/metrics`)等都将对外公开可被任意客户端调用[^1]。 #### 测试未授权访问 一旦上述设置完成并且服务器运行正常,则可以直接通过浏览器或者命令行工具(比如curl)尝试请求不同的Actuator路径。例如获取当前系统的环境属性列表: ```bash curl http://localhost:<port>/actuator/env ``` 如果确实存在权限控制缺失的情况,那么即使不提供任何形式的身份验证凭证也能够成功读取到返回的数据,这表明已经成功复现了未授权访问的问题[^2]。 为了避免潜在的安全风险,在实际生产环境中应当按照最佳实践部署适当的安全策略来防止此类问题的发生,比如采用基于角色的访问控制系统(RBAC),仅允许特定IP地址范围内的设备发起请求等方式加强防护力度[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值