【问题标题】:How to create a spring boot app with ssl.enable=true and a non-secure "/health" end point如何使用 ssl.enable=true 和非安全“/health”端点创建 Spring Boot 应用程序
【发布时间】:2018-10-22 11:26:57
【问题描述】:

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

设置属性时:

server.ssl.enabled=true

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

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

【问题讨论】:

    标签: spring spring-boot https embedded-jetty


    【解决方案1】:

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

    这是我的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();
        }
    }
    

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

    【讨论】:

      【解决方案2】:

      用于禁用管理服务器 TLS 的 Spring Boot 2 属性是:

      management.server.ssl.enabled=false
      

      【讨论】:

      • 但是,如果 server.ssl.client-auth=NEED 和 server.ssl.enabled=true,属性 management.server.ssl.enabled=false 将不起作用,并且端点仍然需要 HTTPS ,如果端口相同,即没有 management.server.port=xxxx override
      猜你喜欢
      • 1970-01-01
      • 2019-05-11
      • 2019-02-03
      • 2021-12-27
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多