spring - 如何配置“健康”执行器端点以使用令牌身份验证?
问题描述
我们在 Spring Boot 2 应用程序中使用基于令牌的身份验证(使用 Spring Security)。现在我正在向它介绍 Spring Boot Actuator。我想将/health
端点配置为在没有任何权限的情况下可见,但仅在获得授权时才显示健康检查详细信息。
我找到了management.endpoint.health.show-details=when_authorized
应该有用的属性,但现在我正在与 Spring Security 配置作斗争,以让每个人都能看到:
{
"status": "UP"
}
在/actuator/health
使用令牌授权的用户应该看到:
{
"status": "UP",
"details": { ... }
}
你遇到过类似的问题吗?你是怎么处理的呢?
解决方案
好的,现在我明白了,如果您关闭应用程序中的安全性并让management.endpoint.health.show-details=when_authorized
您获得公正status
的领域?如果我是对的,这不是问题,请查看 spring 类中的HealthWebEndpointResponseMapper
方法map
。我发现details
如果条件if
为真,此方法将覆盖(从响应中删除字段):
public WebEndpointResponse<Health> map(Health health, SecurityContext securityContext,
ShowDetails showDetails) {
if (showDetails == ShowDetails.NEVER
|| (showDetails == ShowDetails.WHEN_AUTHORIZED
&& (securityContext.getPrincipal() == null
|| !isUserInRole(securityContext)))) {
health = Health.status(health.getStatus()).build();
}
Integer status = this.statusHttpMapper.mapStatus(health.getStatus());
return new WebEndpointResponse<>(health, status);
}
在您的情况下,我猜您已将上述属性设置为when_authorized
也已关闭身份验证,因此主体为空。不确定我是否正确,但我希望我能给你一个线索。:)
推荐阅读
- typescript - 这个信号在 TypeScript 中是什么意思?
- google-chrome-extension - 通过 Chrome 扩展存储中的嵌套对象处理特定的选项卡状态?
- class - 在 VC++ 类中使用回调的编译器错误 C3867
- c++ - 有没有一种简单的方法可以从安装我的程序的通用自定义位置加载所有 c 运行时 dll(例如 vcruntime140.dll)?
- azure-sql-database - Azure 流分析“InputDeserializerError.InvalidData”错误
- webrtc - 我们可以为 STUN / TURN 服务器使用与 'stun'/'turn' 不同的子域吗?
- python - Pentaho:大数据获取和更新 - 输出性能
- ms-access - 有时不显示表单中表达式字段的内容
- swift - RNCryptor - 无法在 django 后端验证加密字符串
- javascript - Split string with regex skipping brackets []