Java Spring boot 2启用非安全/健康端点
我有一个带有客户端和服务器身份验证的Spring Boot 2项目,我试图只公开Java Spring boot 2启用非安全/健康端点,java,spring-boot,ssl,spring-security,spring-boot-actuator,Java,Spring Boot,Ssl,Spring Security,Spring Boot Actuator,我有一个带有客户端和服务器身份验证的Spring Boot 2项目,我试图只公开/exactor/health端点,因此它不需要任何身份验证 我最初的WebSecurity配置适配器是: @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { ... private void configureWithSsl(@NotNull HttpSecurity http) thro
/exactor/health
端点,因此它不需要任何身份验证
我最初的
WebSecurity配置适配器是:
@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());
}
将匹配器添加到端点:
将忽略添加到端点:
我已经尝试了所有这些组合,但我仍然
curl -k https://localhost:8443/actuator/health
curl: (35) error:1401E412:SSL routines:CONNECT_CR_FINISHED:sslv3 alert bad certificate
我只希望健康端点不受保护,其余的执行器端点也需要保护,因此配置management.server.ssl.enabled=false
不会解决问题
编辑:
根据@Aritra Paul answer,我也尝试过:
http
.csrf().disable()
.authorizeRequests()
.antMatchers( "/actuator/health/**").permitAll()
.antMatchers( "/**").authenticated()
.and().x509()
.and().userDetailsService(userDetailsService());
但我仍然得到了相同的结果。据我所知,您希望将执行器/health
url对所有人开放,而所有其他url都基于角色或授权。如果是这样,你可以像贝娄一样尝试
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/actuator/health/**").permitAll()
.antMatchers("/other-url/**").hasRole("your role")
.authenticated();
}
我有一个非常类似的例子,我们在端点中添加了X.509
身份验证,但希望保持Spring执行器端点的身份验证自由
使用Spring 2.2.3,可以像以前一样使用ssl配置服务器
,但在管理
配置(用于配置执行器端点)中禁用ssl,如下所示:
management:
server:
port: 8080
ssl:
enabled: false
这个简单的组合允许我们在端口8080上使用未经身份验证的方式命中执行器端点,而在端口8443上使用ssl命中其他端点。不清楚您的意思是“非ssl”还是“无Spring安全”。我一直在尝试所有端点都将通过https://和身份验证,并且/exactor/health端点不需要任何身份验证。我在原始问题中也试图澄清这一点。您似乎忽略了证书错误和应用程序身份验证错误之间的区别。您介意详细说明吗?我在这里的知识确实有限,我很乐意学习。你找到了解决方案吗?我编辑了我的问题,包括你写的内容,但有一些不同:我也需要x509
(客户端和服务器证书),我不确定什么是@enableSourceServer
,似乎我没有导入该注释所需的依赖项。我是否需要该注释而不是@EnableWebSecurity
?websecurityconfigureadapter
和ResourceServerConfigurerAdapter
都是不同的东西。只需将“configureWithSsl”替换即可。我已经更新了我的答案,这就是我所做的,除了x509()
部分之外,我必须在那里添加antMatchers(“/exactor/health/**”)。permitAll()
和`antMatchers(“/**”)。authenticated()`是矛盾的,那么如何实现呢“除了执行器/运行状况
,所有端点都需要身份验证?”.antMatchers(“/other url/**”)
可能会使其他端点不安全。
http
.csrf().disable()
.authorizeRequests()
.antMatchers( "/actuator/health/**").permitAll()
.antMatchers( "/**").authenticated()
.and().x509()
.and().userDetailsService(userDetailsService());
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/actuator/health/**").permitAll()
.antMatchers("/other-url/**").hasRole("your role")
.authenticated();
}
management:
server:
port: 8080
ssl:
enabled: false