【问题标题】:Spring Boot Admin: custom header authenticationSpring Boot Admin:自定义标头身份验证
【发布时间】:2019-09-06 11:40:05
【问题描述】:

我的应用具有基于自定义 HTTP 标头的自定义身份验证机制。 AFAIK,Spring Boot Admin 仅支持基本身份验证和 OAuth。但也许客户有办法在他们的请求中提供一些自定义标头?

【问题讨论】:

    标签: spring-boot-admin


    【解决方案1】:

    您可以通过注入 Bean 将自定义标头添加到现有标头中,如下所示。

    @Bean
    public HttpHeadersProvider customHttpHeadersProvider() {
        return instance -> {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("X-CUSTOM", "My Custom Value");
            return httpHeaders;
        };
    }
    

    【讨论】:

    • 这不起作用。我已经用我的头文件声明了这个bean,但是这个头文件没有到达de.codecentric.boot.admin.client.registration.ApplicationRegistrator。事实上,我查看了ApplicationRegistrator 代码,找不到任何可以使用该 bean 的地方。
    【解决方案2】:

    好的,所以如果 SBA Server 和 SBA Client 都与被监控的应用程序本身一起启动,并且它具有自定义标头安全性,我们需要注意 3 件事:

    1. 正如尼丁所说,需要注册HttpHeadersProvider bean:
        @Bean
        public HttpHeadersProvider customHttpHeadersProvider() {
            return instance -> {
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("X-CUSTOM", "My Custom Value");
                return httpHeaders;
            };
        }
    
    1. 请注意,这些标头不适用于对 Actuator 端点的 OPTIONS 请求,因此需要自定义 ProbeEndpointsStrategy,或禁用 Spring Security 以实现对管理 URL 的 OPTIONS 调用。此外,出于某种原因,我不得不禁用 /actuator/health/** 的安全性,尽管它应该可以通过提供的自定义标头访问:
        @Override
        public void configure(WebSecurity web) {
            web.ignoring().antMatchers(HttpMethod.OPTIONS, "/actuator/**").antMatchers(HttpMethod.GET, "/actuator/health/**");
        }
    
    1. 最后,需要使用自定义的RestTemplate 来实例化ApplicationRegistrator,该RestTemplate 将预先填充自定义标头:
        @Bean
        public ApplicationRegistrator registrator(ClientProperties client, ApplicationFactory applicationFactory) {
            RestTemplateBuilder builder = new RestTemplateBuilder()
                    .setConnectTimeout(client.getConnectTimeout())
                    .setReadTimeout(client.getReadTimeout())
                    .additionalInterceptors((request, body, execution) -> {
                        request.getHeaders().set("X-CUSTOM", "My Custom Value");
                        return execution.execute(request, body);
                    });
            if (client.getUsername() != null) {
                builder = builder.basicAuthentication(client.getUsername(), client.getPassword());
            }
            return new ApplicationRegistrator(builder.build(), client, applicationFactory);
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2017-03-17
      • 2021-04-21
      • 2019-02-21
      • 2019-04-09
      • 1970-01-01
      • 2014-07-29
      • 2017-11-21
      • 1970-01-01
      相关资源
      最近更新 更多