Spring boot 2 enabling a non-secure /health endpoint

api*_*nes 5 java ssl spring-security spring-boot spring-boot-actuator

我有一个带有客户端和服务器身份验证的 Spring Boot 2 项目,我试图只公开/actuator/health端点,因此它不需要任何身份验证。

我的最初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());
    }
...
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我尝试过: 1. 将“通道”配置为端点不安全

    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());
    }
Run Code Online (Sandbox Code Playgroud)
  1. 将匹配器添加到端点:
    private void configureWithSsl(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .requestMatchers(EndpointRequest.to("health")).permitAll()
                .anyRequest().authenticated()
                .and().x509()
                .and().userDetailsService(userDetailsService());
    }
Run Code Online (Sandbox Code Playgroud)
  1. 将忽略添加到端点:
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().requestMatchers(EndpointRequest.to("health"));
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有这些组合,但我仍然得到

curl -k  https://localhost:8443/actuator/health
curl: (35) error:1401E412:SSL routines:CONNECT_CR_FINISHED:sslv3 alert bad certificate
Run Code Online (Sandbox Code Playgroud)
curl http://localhost:8443/actuator/health
Bad Request
This combination of host and port requires TLS.
Run Code Online (Sandbox Code Playgroud)

我只想不保护健康端点,我需要保护其余的执行器端点,因此配置management.server.ssl.enabled=false不会解决问题......

编辑:

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

http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers( "/actuator/health/**").permitAll()
        .antMatchers( "/**").authenticated()
        .and().x509()
        .and().userDetailsService(userDetailsService());
Run Code Online (Sandbox Code Playgroud)

但我仍然得到相同的结果。

Unc*_*air 5

我有一个非常相似的案例,我们向X.509端点添加了身份验证,但希望保持 Spring 执行器端点免身份验证。

使用 Spring 2.2.3,可以server像您一样使用 ssl进行配置,但在您的management配置(用于配置执行器端点)中禁用 ssl,如下所示:

management:
  server:
    port: 8080
    ssl:
      enabled: false
Run Code Online (Sandbox Code Playgroud)

这种简单的组合使我们能够在端口 8080 上访问没有身份验证的执行器端点,而我们在端口 8443 上使用 ssl 访问其他端点。


Ari*_*aul -1

据我了解,您希望对actuator/health所有人开放 url,并且所有其他 url 是基于角色或授权的。如果是这样你可以尝试如下

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                .antMatchers( "/actuator/health/**").permitAll()
                .antMatchers("/other-url/**").hasRole("your role")
                .authenticated();
    }
Run Code Online (Sandbox Code Playgroud)