【问题标题】:Enabling Spring Boot Health Actuator with OAuth2 Authentication使用 OAuth2 身份验证启用 Spring Boot Health Actuator
【发布时间】:2017-10-25 03:28:44
【问题描述】:

我们已经看到了许多与 Spring Boot 1.5+ 版本的 Health Actuator 端点相关的问题。经历了其中的一些,我们仍然不知所措。

我们的目标是:

  1. 利用 Spring Boot/Spring Security 的安全过滤器链的自动配置(即不必完全实现/配置 HttpSecurity
  2. 启用安全健康访问(查看应用健康信息的完整视图)
  3. 启用不安全健康访问(允许端点在 Kubernetes 中充当活性探针)

我们已经尝试过:

  1. 使用WebSecurityConfigurerAdapter 并配置WebSecurity 对象以忽略/health 端点的安全性,然后根据Routing multiple URLs to Spring Boot Actuator's health endpoint 将单独的端点映射到/health 端点以希望启用安全路径。
  2. 确保security.oauth2.resource.filter-order=3 符合https://github.com/spring-projects/spring-boot/issues/5072 中的建议。这会将 OAuth2AuthenticationProcessingFilter 放在 Actuator 的 Mvc 端点之前,并允许处理包含预先验证的 Authorization: Bearer ... 标头(例如 JWT 授权)的请求。但是,它规定所有请求都包含授权 - 否则,FilterSecurityInterceptor 触发 Secure object: FilterInvocation: URL: /health; Attributes: [#oauth2.throwOnError(authenticated)]AccessDeniedException

/health 使用基本身份验证,对其他一切使用OAuth2 是不行的(请参阅Spring boot oauth2 management httpbasic authenticationhttps://github.com/spring-projects/spring-boot/issues/5072)。

我们不断回到的问题是我们如何得到:

  • /health 端点的匿名请求作为不安全
  • /health 端点的预认证请求(即包含预认证Authorization: Bearer ... 标头的请求)没有适当的授权或角色作为不安全
  • /health端点的预认证请求(即那些包含预认证Authorization: Bearer ...标头的请求)具有适当的授权或角色以充当安全

我们可以通过以下方式轻松允许任何请求访问/health

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  public void configure(WebSecurity web) throws Exception {
    super.configure(web);
    web.ignoring().antMatchers(HttpMethod.GET, "/health", "/info");
  }
}

这仅作为准备就绪/活跃度探测非常有效。但是,当用户实际通过身份验证时,它并没有提供查看哪些支持服务可能行为不端的好处。

提前致谢!

【问题讨论】:

  • 我不确定我是否完全理解这些问题。您是否只想将 Spring Security 与 Spring Actuator 一起使用并只允许所有用户进行 /health 检查?!我的意思是只允许任何请求访问/health?!

标签: spring-boot spring-security jwt spring-security-oauth2 spring-boot-actuator


【解决方案1】:

试试这个,为我工作

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/**").authorizeRequests()
            .antMatchers("/actuator/**","/assets/**")
            .permitAll()
            ........
    ;

}

【讨论】:

    【解决方案2】:

    我遇到了类似的事情,这有点工作: 为执行器添加一个 hacky 访问规则,例如:#oauth2.clientHasRole('ROLE_CLIENT') 或 hasRole('ROLE_ANONYMOUS'),它允许为执行器端点填充安全上下文(对于经过身份验证和未经身份验证的请求)并调整“敏感的执行器端点配置。 在这种情况下,/health 应该返回匿名的基本信息和经过身份验证的完整信息,前提是您启用管理安全性并将其标记为非敏感。 您仍然需要保留过滤器配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-26
      • 2016-09-01
      • 2017-10-01
      • 2017-10-08
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      相关资源
      最近更新 更多