SpringBoot - 集成Actuator(应用信息显示、修改系统日志、增加账号密码登录)
创始人
2024-03-03 22:48:02
0

文章目录

    • Actuator
      • 概述
        • 官网入口
        • 支持的埋点信息查询、修改
      • 使用
        • 访问actuator埋点信息添加账号密码登录验证
        • 动态修改日志级别

Actuator

概述

官网入口

官网: https://docs.spring.io/spring-boot/docs/2.7.6/reference/html/actuator.html#actuator.endpoints

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

支持的埋点信息查询、修改

官网: https://docs.spring.io/spring-boot/docs/2.7.6/reference/html/actuator.html#actuator.endpoints

访问:

  1. 获取信息(Get):http://ip:端口号/actuator/埋点ID
  2. 修改信息(Post): http://ip:端口号/actuator/埋点ID/[[可能会有,比如修改日志时需要使用到]]

在这里插入图片描述

在这里插入图片描述

使用

访问actuator埋点信息添加账号密码登录验证

特别注意: 不要使用拦截器(HandlerInterceptor)进行拦截/actuator链接,因为拦不到,只能使用过滤器才能拦截到,我也不知道为什么

pom.xml - 依赖

org.springframework.bootspring-boot-starter-actuator


application.yml

management:endpoint:beans:enabled: truehealth:# 不开启仅有应用是否可用信息,开启了可以看到磁盘信息、数据库连接是否可用状态等其他组件信息show-details: alwaysshutdown:enabled: trueendpoints:# 所有端点功能设置成默认都可用 == 开启了这个上面的配置如果不是false,可以不用设置enabled-by-default: trueweb:cors:allow-credentials: trueallowed-methods: '*'allowed-origin-patterns: '*'exposure:# 所有端点都对外暴露可访问到include: '*'



LrcUtilsWebApplication.java - 启动类-必须必须添加注解@ServletComponentScan,否则过滤器的路径匹配不生效

@ServletComponentScan
@SpringBootApplication
public class LrcUtilsWebApplication {public static void main(String[] args) {SpringApplication.run(LrcUtilsWebApplication.class, args);}}



ActuatorEndpointFilter.java - 账号密码过滤器

/*** @author LinRuChang* @version 1.0* @date 2022/11/30* @since 1.8**/
@Slf4j
@WebFilter(filterName = "actuatorEndpointFilter", urlPatterns = {"/actuator/*"})
public class ActuatorEndpointFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {//无需登录,可直接访问、修改埋点信息if(!SpringUtil.getBean(SysDictService.class).getAuthActuatorSwitch()) {chain.doFilter(request, response);return;}boolean authStatusFlag = false;String authorization = EnhanceSpringUtil.getCurrentRequestHeader("Authorization");log.info("Actuator系统埋点认证信息【Authorization】:{}", authorization);String authType = null;String authInfoCredentials = null;String userName = null;List authorizationInfos = StrUtil.splitTrim(authorization, StrUtil.SPACE);if (CollUtil.size(authorizationInfos) == 2) {authType = authorizationInfos.get(0);authInfoCredentials = authorizationInfos.get(1);if (StrUtil.isNotBlank(authInfoCredentials)) {if (StrUtil.equalsAnyIgnoreCase(authType, "BASIC")) {String authInfo = Base64.decodeStr(authInfoCredentials);List authInfos = StrUtil.splitTrim(authInfo, StrUtil.COLON);userName = CollUtil.size(authInfos) >= 2 ? authInfos.get(0) : null;String password = CollUtil.size(authInfos) >= 2 ? authInfos.get(1) : null;// 这里自行获取数据库中的用户、密码信息进行校验是否正确Dict dbUserInfo = getActuatorUserInfo(userName);if (dbUserInfo != null && StrUtil.equals(password, dbUserInfo.getStr("password"))) {authStatusFlag = true;}}}}// 认证失败if (!authStatusFlag) {HttpServletResponse httpServletResponse = (HttpServletResponse) response;httpServletResponse.setHeader("WWW-Authenticate", StrUtil.format("{} realm=\"请填写正确的Actuator管理员账号、密码信息\"", StrUtil.blankToDefault(authType, "BASIC")));httpServletResponse.setStatus(401);httpServletResponse.setHeader("Content-Type", RequestEnum.RequestContentTypeEnum.JSON.getCode());ResponseResult unauthorizedInfo = ResponseResult.unauthorized("请填写正确的Actuator管理员账号、密码信息");httpServletResponse.getWriter().write(JSONUtil.toJsonStr(unauthorizedInfo));return;}chain.doFilter(request, response);}public Dict getActuatorUserInfo(String loginName) {SysDictService sysDictService = SpringUtil.getBean(SysDictService.class);Dict actuatorConfig = sysDictService.getActuatorConfig();if (StrUtil.equals(loginName, actuatorConfig.getStr("loginName"))) {return actuatorConfig;}return null;}}

在这里插入图片描述


动态修改日志级别

日志级别(某日志级别会把其上面级别的日志也会显示出来)OFFERRORINFOINFODEBUGTRACE
//post请求//全局日志级别修改
http://127.0.0.1:9999/actuator/loggers/ROOT
//请求体
{"configuredLevel": "日志级别(忽略大小写)"
}//局部日志级别修改
http://127.0.0.1:9999/actuator/loggers/类、包全限定名
//请求体
{"configuredLevel": "日志级别(忽略大小写)"
}

在这里插入图片描述

相关内容

热门资讯

【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
【前端】‘??‘与‘||‘有什... 0 问题 经常写const data = res.data.a ?? ''或者const d...
ChatGPT 怎么用最新详细... ChatGPT 以其强大的信息整合和对话能力惊艳了全球,在自然语言处理上面表现出了惊人...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...