Mysticbinary

springboot actuator 配置安全


springboot actuator监控是什么?类似php的phpinfor()函数,不过actuator更强大,可以查看的数据、状态更多。Actuator是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans信息、系统环境变量的配置信以及Web请求的详细信息等。如果使用不当或者一些不经意的疏忽,可能造成信息泄露等严重的安全隐患。

使用

pom加入依赖

<!--actuator模块为SpringBoot提供一系列用于监控的端点-->
<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter-actuator</artifactId>  
</dependency> 

pom加入依赖(安全版)

<dependencies>
    <!-- 如果使用http调用的方式,还需要这个依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 必须的 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- 安全需要,为了保证actuator暴露的监控接口的安全性,需要添加安全控制的依赖spring-boot-start-security依赖,访问应用监控端点时,都需要输入验证信息。-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

properties配置文件

在application.properties核心配置文件中除了定义数据源外,还需要添加 management.security.enabled=false 配置。
不加的话,访问监控路径会报401。

#########################################################
###   Actuator Monitor  --   Actuator configuration   ###
#########################################################
management.security.enabled=false

在SpringBoot的application.yml配置文件中加入

# ============================= actuator监控 ============================= #
management:
  server:
    port: 1234           # 管理的端口调整成1234
    address: 127.0.0.1   # 只允许127.0.0.1访问
    servlet:
      context-path: /monitor  # actuator的访问路径
  endpoint:
    shutdown:
      enabled: true    # 启用shutdown功能
    beans.cache.time-to-live: 10s
    env.enabled: true  # 启用端点 env
  endpoints:
    enabled-by-default: true # 设置端点是否可用 默认只有shutdown可用
    web:
      # 设置是否暴露端点 默认只有health和info可见
      exposure:
        # include: env   # 方式1: 暴露端点 env 配置多个,隔开
        include: "*"     # 方式2: 包括所有端点, 注意需要添加引号
        # 排除端点
        exclude: shutdown

注意:若在核心配置文件中未添加 management.security.enabled=false 配置,将会导致用户在访问部分监控地址时访问受限,报401未授权错误。


常见的监控项目

方法 |路径| 描述
-|-|-|-
GET |/autoconfig |查看自动配置的使用情况
GET |/conditions |提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
GET |/configprops |描述配置属性(包含默认值)如何注入Bean
GET |/beans |描述应用程序上下文里全部的Bean,以及它们的关系
GET |/dump |打印线程栈
GET |/heapdump |获取堆的快照
GET |/threaddump |获取线程活动的快照
GET |/env |获取全部环境属性
GET |/env/{name} |根据名称获取特定的环境属性值
GET |/health |报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
GET |/info |获取应用程序的定制信息,这些信息由info打头的属性提供
GET |/mappings |描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
GET |/metrics |报告各种应用程序度量信息,比如内存用量和HTTP请求计数
GET |/metrics/{name} |报告指定名称的应用程序度量值
POST |/shutdown |关闭应用程序,要求endpoints.shutdown.enabled设置为true
GET |/trace |提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

加入了依赖之后,在外部是可以访问到如下路径的:

# 加入上述依赖后,默认可以访问的url
http://localhost:8080/actuator
http://localhost:8080/actuator/info
http://localhost:8080/actuator/health

有敏感数据的接口可以自己测试一下:

/autoconfig
/conditions
/configprops
/beans
/heapdump
/threaddump
/env
/info
/mappings
/metrics
/trace

参考

https://xz.aliyun.com/t/2233
https://www.freebuf.com/news/193509.html
https://blog.csdn.net/qq_29668759/article/details/98672900

posted on 2020-04-13 10:21  Mysticbinary  阅读(4594)  评论(0编辑  收藏  举报

导航