Spring boot 2启用非安全/健康端点

vjhs03f7  于 2022-09-19  发布在  Spring
关注(0)|答案(3)|浏览(194)

我有一个带有客户端和服务器身份验证的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.将“通道”配置为端点不安全

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());
    }

1.向端点添加匹配器:

private void configureWithSsl(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .requestMatchers(EndpointRequest.to("health")).permitAll()
                .anyRequest().authenticated()
                .and().x509()
                .and().userDetailsService(userDetailsService());
    }

1.将忽略添加到端点:

@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不会解决问题。。。

编辑:

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

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

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

anauzrmj

anauzrmj1#

我有一个非常类似的案例,我们在端点添加了X.509身份验证,但希望保持Spring致动器端点身份验证免费。
使用Spring 2.2.3,可以像之前一样使用ssl配置server,但在management配置(用于配置致动器端点)中禁用ssl,如下所示:

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

这个简单的组合允许我们在端口8080上使用无身份验证的情况下访问致动器端点,而在端口8443上使用ssl访问其他端点。

gywdnpxw

gywdnpxw2#

我的解决方法如下:
在实施WebSecurityConfigurerAdapterSecurityConfig

@Override
    protected void configure(HttpSecurity http) throws Exception {
    http.regexMatcher("^(?!(/actuator/)).*$")
            .csrf().disable()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .x509()
            .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
            .userDetailsService(userDetailsService());

application.yaml

management:
  server:
    port: 8088
    ssl:
      enabled: false
  endpoint:
    health:
      probes:
        enabled: true
  health:
    livenessState:
      enabled: true
    readinessState:
      enabled: true
ffscu2ro

ffscu2ro3#

据我所知,您希望使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();
    }

相关问题