声明:文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途给予盈利等目的,否则后果自行承担!
actuator 1.x和2.x的区别
https://bin4xin.sentrylab.cn/blog/2020/Spring-boot/
- Spring Boot 1.x 版本默认内置路由的根路径以 / 开始,2.x 则统一以 /actuator 开始
- spring boot 1.x 的Actuator Web所有端点是默认开启的, 可以直接使用。spring boot 2.x 的Actuator Web端点只有/health和/info默认开启, 如果还想访问其它端点, 需要在配置文件application.properties中配置开启
路由知识:
- 有些程序员会自定义 /manage、/management 或 项目相关名称 为根路径
- 默认内置路由名字,如 /env 有时候也会被程序员修改,如修改成 /appenv
注意 Spring Boot Actuator 1.x 版本主要与 Spring Boot 1.x 系列版本相匹配。Spring Boot 2.x 与 Actuator 2.x 匹配。如果Spring Boot 2.x用了Actuator 1.x,web服务可以正常启动,但actuator不会工作。
https://kimi.moonshot.cn/
如果你需要具体到某个版本的 Spring Boot 与 Actuator 的对应关系,通常可以通过查看 Spring Boot 的版本发布说明或者在 Maven 中央仓库中搜索对应版本的 spring-boot-starter-actuator
依赖来获取详细信息。
actuator 2.x 使用详情
https://juejin.cn/post/6909651340193562632
在application.properties配置
选择若干个要开启的端点
management.endpoints.web.exposure.include=["auditevents","health","info"]
开启全部端点
management.endpoints.web.exposure.include=*
如果想要enable /shutdown端点
management.endpoint.shutdown.enabled=true
/shutdown 端点是 Spring Boot Actuator 模块提供的一个操作控制类端点,它允许用户通过发送 HTTP POST 请求来优雅地关闭应用程序 。默认情况下,出于安全考虑,/shutdown 端点是禁用的,需要在
application
.properties 或
application
.yml 文件中通过设置
演示
查看服务,已关闭
如果想要暴露所有enable的web端点除了env
management.endpoints.web.exposure.include
=*
management.endpoints.web.exposure.exclude
=env
在yaml 文件属于关键字,所以需要加引号
management:
endpoints:
web:
exposure:
include:
"*"
演示
搭建环境
pom.xml加入依赖
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-actuator</
artifactId
>
</
dependency
>
app启动后,访问
默认只有几个,没有env
查了资料,actuator的高版本默认是关闭的,需要手动开启
management
.endpoints
.web
.exposure
.include=*
此时就变多了
Actuator端口保护
https://blog.csdn.net/weixin_43522117/article/details/134312877
Actuator 访问ip和端口
https://blog.csdn.net/weixin_43522117/article/details/134312877
management.server.address
=
127.0
.
0.1
management.server.port
=
8888
http://127.0.0.1:8888/actuator
spring-boot-starter-security 限制路由
添加Spring Security依赖
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-security</
artifactId
>
</
dependency
>
Spring Security简单配置类
package com.example.highactuator.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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 SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin() // 自定义自己编写的登录页面
.loginPage("/test/login") // 登录页面设置
.loginProcessingUrl("/test/login") // 登录访问路径
.defaultSuccessUrl("/test/index").permitAll() // 登录成功后的跳转路径
.and().authorizeRequests()
.antMatchers("/high","/high/**").hasRole("ADMIN")
.antMatchers("/", "/test/hello", "/login.html").permitAll() // 设置哪些路径可以直接访问,不需要认证
.anyRequest().authenticated()
.and().csrf().disable(); // 关闭csrf防护
}
}
application.yml配置规则
配置设置了一个具有用户名 admin 和密码 password 的管理员用户,并且仅当用户提供正确的用户名和密码后,才会显示详细的健康信息。以及health的映射。
spring
:
security
:
user
:
name
:
admin
password
:
admin
roles
:
ADMIN
management
:
endpoint
:
health
:
show-components
:
when_authorized
endpoints
:
web
:
base-path
:
/high
path-mapping
:
health
:
heal
修复方案
nginx 配置文件
屏蔽actuator路径
location
~ .*actuator
.* {
deny
all;
}
k8s-ingress 配置文件
屏蔽actuator路径
metadata:
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
location
~ .*actuator
.* {
deny
all;
}
Actuator 开启认证授权(存在代码污染风险)
主要是借助 spring security 来实现的,如果当前项目有使用security,需要解决合并问题
关闭端口
大多数是不会采取这种方案,但也不排除生产上感觉actuator没什么实质作用,可以选择关闭暴露端点
毕竟使用actuator是主观意向,主要是为了配合 SpringAdmin 或 Prometheus 来监控的,很多人都会需求其他的解决方案
在配置文件中添加配置
management:
endpoints:
enabled-by-default:
false
# 版本低的配置
# management:
# endpoints:
# enabled: false
网关限制(最简单粗暴)
如果使用nginx作为网关控制,直接屏蔽掉相关接口即可。
引入spring security
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-security</
artifactId
>
</
dependency
>
引入后,会自动在用户访问前验证身份
默认账号为user
默认密码为生成的密码
密码在log中
参考链接
https://www.cnblogs.com/chanshuyi/p/06_prometheus_with_springboot_actuator.html
https://springdoc.cn/spring-boot-actuators
https://liaoxuefeng.com/books/java/springboot/actuator/index.html
https://blog.csdn.net/qq_42914528/article/details/131388401
https://blog.csdn.net/qq_34491508/article/details/134600628
https://bbs.huaweicloud.com/blogs/281509
原文始发于微信公众号(进击的HACK):分析Actuator未授权出现的原因
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论