当前位置: 移动技术网 > IT编程>开发语言>Java > springboot actuator 配置安全

springboot actuator 配置安全

2020年04月13日  | 移动技术网IT编程  | 我要评论

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

参考



如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网