如何使用 ssl.enable=true 和非安全的“/health”端点创建 Spring Boot 应用程序

tod*_*car 5 https spring embedded-jetty spring-boot

是否可以将 Spring Boot 应用程序( Jetty )配置为至少有一个非安全(非 https)端点,以便负载均衡器执行运行状况检查,但所有其他请求都必须强制安全?

设置属性时:

server.ssl.enabled=true

对所有端口(常规端口和管理/执行器端口)的请求都强制为 https。

安全请求 URL 中的服务器名称必须与配置的证书匹配。像 kubernetes 这样的负载均衡器或容器管理器必须使用某种主机名到服务器的映射来访问服务器池中的每个节点。

mjj*_*409 1

最初我以为这个设置management.ssl.enable=false可以解决问题,但事实似乎并非如此。我最终所做的对我有用的事情是仅为/health端点添加 ssl 排除规则。

这是 my 的删节版本SecurityConfiguration,它是一个@Configuration扩展/实现的带注释的类WebSecurityConfigurerAdapter/WebSecurityConfigurer

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/secure-path").hasAuthority("SOME_ROLE")
            .anyRequest().authenticated()
            .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
            .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login")
                .permitAll()
            .and()
                .exceptionHandling();



    if (securityProperties.isRequireSsl()) {
        //allow health checks to be over http
        http.requiresChannel().antMatchers("/health").requiresInsecure();
        http.requiresChannel().anyRequest().requiresSecure();
    }
}
Run Code Online (Sandbox Code Playgroud)

利用requiresInsecure()端点/health是关键。请注意,顺序很重要,通常在 Spring Security 中更具体的规则应该放在第一位。