是否可以将Spring引导应用程序(Jetty)配置为至少有一个非安全(非https)端点,以便负载均衡器执行运行状况检查但是强制所有其他请求是否安全?
设置属性时:
server.ssl.enabled=true
所有端口(常规端口和管理/执行器端口)的请求都被强制为https。
安全请求URLS必须使URL中的服务器名称与配置的证书匹配。像kubernetes这样的负载均衡器或容器管理器必须访问具有某种主机名到服务器映射的服务器池中的每个节点。
最初我认为设置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中应该首先提出更具体的规则。