基于SpringBoot应用监控Actuator安全隐患及解决方式

网友投稿 1323 2022-12-25

基于SpringBoot应用监控Actuator安全隐患及解决方式

基于SpringBoot应用监控Actuator安全隐患及解决方式

概述

微服务作为一项在云中部署应用和服务的新技术是当下比较热门话题,而微服务的特点决定了功能模块的部署是分布式的,运行在不同的机器上相互通过服务调用进行交互,业务流会经过多个微服务的处理和传递,在这种框架下,微服务的监控显得尤为重要。

而Actuator正是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans信息、系统环境变量的配置信息以及Web请求的详细信息等。

如果使用不当或者一些不经意的疏忽,可能造成信息泄露等严重的安全隐患。

Actuator使用

Actuator应用监控使用只需要添加spring-boot-starter-actuator依赖即可,如下:

org.springframework.boot

spring-boot-starter-actuator

可以在application.properties中指定actuator的访问端口、访问路径等信息:

# 访问示例:http://localhost:9595/monitor

managbzlRSopement:

endpoints:

web:

# actuator的访问路径,替换默认/actuator

base-path: /monitor

# 设置是否暴露端点 默认只有health和info可见

exposure:

# include: env # 方式1: 暴露端点env,配置多个以,隔开

include: "*" # 方式2: 包括所有端点,注意http://需要添加引号

# 排除端点

exclude: shutdown

server:

port: 9595 #新开监控端口,不和应用用同一个端口

endpoint:

health:

show-details: always # 显示db、redis、rabbti连接情况等

shutdown:

enabled: true #默认情况下,除shutdown以外的所有端点均已启用。手动开启

此时,运行示例,访问/monitor/即可查看所有端点信息,再访问/monitor/env即可查看该应用全部环境属性,如图:

Endpoints(端点)介绍

Endpoints 是 Actuator 的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们扩展自己的端点。

Endpoints 分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。

原生端点是在应用程序里提供的众多 restful api 接口,通过它们可以监控应用程序运行时的内部状况。原生端点又可以分成三类:

应用配置类:可以查看应用在运行期间的静态信息:例如自动配置信息、加载的spring bean信息、yml文件配置信息、环境信息、请求映射信息;

度量指标类:主要是运行期间的动态信息,例如堆栈、请求连、一些健康指标、metrics信息等;

操作控制类:主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭。

Actuator 默认提供了以下接口,具体如下表所示:

ID

描述

默认启用

默认公开

auditevents

公开当前应用程序的审计事件信息

Yes

No

beans

显示应用程序中所有Spring bean的完整列表

Yes

No

conditions

显示在配置和自动配置类上评估的条件以及它们是否匹配的原因

Yes

No

configprops

显示所有@ConfigurationProperties对照的列表

Yes

No

env

从Spring的ConfigurableEnvironment中公开属性

Yes

No

flyway

显示已应用的任何Flyway数据库迁移

Yes

No

health

显示应用程序健康信息

Yes

Yes

httptrace

显示HTTP跟踪信息(默认情况下,最后100个HTTP请求-响应交互)

Yes

No

info

显示任意应用程序信息

Yes

Yes

loggers

显示和修改应用程序中记录器的配置

Yes

No

liquibase

显示已应用的任何Liquibase数据库迁移

Yes

No

metrics

显示当前应用程序的“指标”信息

Yes

No

mappings

显示所有@RequestMapping路径对照的列表

Yes

No

scheduledtasks

显示应用程序中调度的任务

Yes

No

sessions

允许从Spring Session支持的会话存储中检索和删除用户会话

Yes

No

shutdown

让应用程序优雅地关闭

No

No

threaddump

执行线程转储

Yes

No

安全措施

如果上述请求接口不做任何安全限制,安全隐患显而易见。实际上Spring Boot也提供了安全限制功能。比如要禁用/env接口,则可设置如下:

endpoint:

env:

enabled: false

另外也可以引入spring-boot-starter-security依赖

org.springframework.boot

spring-boot-starter-security

在application.properties中开启security功能,配置访问权限验证,这时再访问actuator功能时就会弹出登录窗口,需要输入账号密码验证后才允许访问。

spring:

security:

user:

password: 123456

name: jaler

为了只对actuator功能做权限验证,其他应用接口不做认证,我们可以重新定制下SpbzlRSopringSecurity

package com.jaler.common.common.config;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration

@EnableWebSecurity

public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

Environment env;

@Override

protected void configure(HttpSecurity security) throws Exception {

String contextPath = env.getProperty("management.endpoints.web.base-path");

if(StringUtils.isEmpty(contextPath)) {

contextPath = "";

}

security.csrf().disable();

security.authorizeRequests()

.antMatchers("/**"+contextPath+"/**")

.authenticatehttp://d()

.anyRequest()

.permitAll()

.and()

.httpBasic();

}

}

再次访问http://localhost:9595/monitor,此时需要进行权限验证,如下图:

安全建议

1.只开放某些无敏感信息的端点。

2.打开安全限制并进行身份验证,访问Actuator接口时需要登录。

3.Actuator访问接口使用独立端口,并配置不对外网开放。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解读一体化政务服务平台(政务服务一体化平台的作用)
下一篇:Spring boot actuator端点启用和暴露操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~