zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Learning Experience of Big Data:The First Day-Try to set up a network connection on my virtural machine
    Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop
    事物总线模式实例——EventBus实例详解
    软件架构——事件总线模式
    阅读《大型网站技术架构》,并结合"重大需求征集系统"有感
    淘宝网的六个质量属性
    读架构漫谈博文有感
    06软件需求读书笔记(六)
    .NET应用程序性能优化
    【转】消息队列设计精要
  • 原文地址:https://www.cnblogs.com/mysticbinary/p/12689743.html
Copyright © 2011-2022 走看看