当前位置: 首页 > 知识库问答 >
问题:

Spring boot 2启用非安全/健康endpoint

阚夕
2023-03-14

我最初的WebSecurityConfigurerAdapter是:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
    private void configureWithSsl(@NotNull HttpSecurity http) throws Exception {
        LOG.info("configuring access with ssl");
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().x509()
                .and().userDetailsService(userDetailsService());
    }
...
}

application.yaml

server:
  port: 8443
  ssl:
    enabled: true
    key-store: 'paht/to/kestore/keystore.jks'
    key-store-password: 123456
    key-store-type: JKS
    client-auth: need
    trust-store: 'paht/to/truststore/truststore.jks'
    trust-store-type: JKS
    trust-store-password: 123456
    protocol: TLS
    enabled-protocols: TLSv1.2

我试过了:1。将“通道”配置为endpoint不安全

    private void configureWithSsl(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .requiresChannel().requestMatchers(EndpointRequest.to("health")).requiresInsecure()
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().x509()
                .and().userDetailsService(userDetailsService());
    }
    null
    private void configureWithSsl(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .requestMatchers(EndpointRequest.to("health")).permitAll()
                .anyRequest().authenticated()
                .and().x509()
                .and().userDetailsService(userDetailsService());
    }
    null
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().requestMatchers(EndpointRequest.to("health"));
}
curl -k  https://localhost:8443/actuator/health
curl: (35) error:1401E412:SSL routines:CONNECT_CR_FINISHED:sslv3 alert bad certificate
curl http://localhost:8443/actuator/health
Bad Request
This combination of host and port requires TLS.

根据@Aritra Paul的回答,我也试过:

http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers( "/actuator/health/**").permitAll()
        .antMatchers( "/**").authenticated()
        .and().x509()
        .and().userDetailsService(userDetailsService());

但我还是得到同样的结果。

共有1个答案

厉坚
2023-03-14

我有一个非常类似的例子,我们在endpoint中添加了X.509身份验证,但希望保持Spring actuatorendpoint没有身份验证。

使用Spring 2.2.3可以像您所做的那样使用ssl配置服务器,但是在管理配置(用于配置执行器endpoint)中禁用ssl,如下所示:

management:
  server:
    port: 8080
    ssl:
      enabled: false

这个简单的组合允许我们在端口8080上使用无auth访问执行器endpoint,而在端口8443上使用ssl访问其他endpoint。

 类似资料:
  • 我正在寻找一种方法来改变Spring Boot的健康检查的头部(添加一些东西到它)。我搜索了这个主题,但我只找到了关于自定义健康功能等的问题(例如,如何在spring boot health中添加自定义健康检查?)。那不是我要找的。 我想使用标准/默认健康功能(因此我不想更改响应的主体(“status”:“up”),也不想实现自己的健康功能或自定义默认功能。我的目标是更改响应的标题以实现两个目标:

  • 我跟随http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html使用Spring Cloud构建分布式系统。 除尤里卡客户健康检查外,所有工作均按预期进行。 我有过 eureka:客户端:healthcheck:启用:true 把我的服务指向不存在的config_server,这导致 超文本传输协议://myservi

  • 我有一个基于SpringBoot的web应用程序,它公开了一个Consor健康指示器bean 在进一步检查后,我发现了负责获取所有可用指标的下面片段(HealthEndpoint Configuration.java): 在这里设置一个断点,我看到ConsultHealthIndicator bean确实没有列在applicationContext的输出中。getBeansOfType(Healt

  • 在设置ELB健康检查的对话框中,它会声明: 如果实例未通过健康检查,它将自动从负载均衡器中删除。自定义健康检查以满足您的特定需要。 当健康检查失败时,将从ELB后面删除实例。我的问题是围绕“健康门槛”设置。当你悬停在帮助上时,它会说: 在声明EC2实例健康之前连续运行状况检查成功的次数。 如果实例声明为健康的,它是否被拉回负载平衡组?

  • Spring Cloud Stream为粘合剂提供健康指标。它以binders的名义注册,可以通过设置management.health.binders.enabled属性启用或禁用。