我有一个具有客户端和服务器身份验证的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());
    }
...
}

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.将“ channel ”配置为端点不安全
    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());
    }
  • 将匹配器添加到端点:
  •     private void configureWithSsl(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .authorizeRequests()
                    .requestMatchers(EndpointRequest.to("health")).permitAll()
                    .anyRequest().authenticated()
                    .and().x509()
                    .and().userDetailsService(userDetailsService());
        }
    
  • 将忽略添加到端点:
  • @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.
    

    我只希望不保护运行状况端点,而我确实需要保护其余的执行器端点,因此配置management.server.ssl.enabled=false将无法解决问题...

    编辑:

    按照@Aritra Paul的回答,我也尝试过:
    http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers( "/actuator/health/**").permitAll()
            .antMatchers( "/**").authenticated()
            .and().x509()
            .and().userDetailsService(userDetailsService());
    

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

    最佳答案

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

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

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

    这种简单的组合使我们能够在端口8080上没有auth的情况下命中执行器端点,而在端口8443上使用ssl的命中其他端点。

    关于java - Spring Boot 2启用非安全/运行状况端点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54859826/

    10-13 09:32