使用 user/password 保护 Actuator 端点,同时授予 RestControllers public 访问权限
Protect Actuator endpoints with user/password while granting public access for RestControllers
我将现有的应用程序从 Spring Boot 1.3 更新到 2.0.1。此应用程序使用 Actuator 并公开 REST 样式 API.
在 Boot 1.3 中,API 无需身份验证即可使用,执行器端点配置为受密码保护:
security.user.name=foo
security.user.password=bar
security-user.role=ADMIN
我按照 configuration changelog 中的记录进行了更新,并将条目从 security.user.name
重命名为 spring.security.user.name
等。
但是当我尝试 curl
我的 API 时,我被拒绝了,因为我没有提供凭据:
在 Spring Blog 中,我找到了如何在详细级别上配置 Spring 安全性的可能解决方案:
http
.authorizeRequests()
// 1
.requestMatchers(EndpointRequest.to("status", "info"))
.permitAll()
// 2
.requestMatchers(EndpointRequest.toAnyEndpoint())
.hasRole("ACTUATOR")
// 3
.requestMatchers(StaticResourceRequest.toCommonLocations())
.permitAll()
// 4
.antMatchers("/**")
.hasRole("USER")
.and()
...
但这比我需要的更细粒度,我正在寻找基于 application.properties
的解决方案。
有没有无需额外代码即可解决此问题的方法?
当您设置 spring.security.user.name
和 spring.security.user.password
时,您正在为整个应用程序(包括 Actuator 端点)配置通过 spring-security
的表单登录。
不幸的是,在 Spring Boot 2.0 中,您无法使用属性设置不同的 username/password 或禁用执行器端点的身份验证。这意味着您必须通过安全配置明确允许执行器端点。
通过 spring-security
,您还可以允许 public 访问您的端点并非常轻松地要求执行器端点的凭据:
@Configuration
public class BasicSecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/**").hasRole("ACTUATOR")
.anyRequest().permitAll();
}
}
(我假设你使用的是 WebMvc,而不是 WebFlux,这有点不同)
确认您在 application.properties
中有以下内容:
spring.security.user.name=user
spring.security.user.password=pass
spring.security.user.roles=ACTUATOR,USER # and others, if you like
management.endpoint.health.roles=ACTUATOR
请参阅 here,了解 Spring 1.x 与 2.0 中 Actuator 差异的快速而准确的解释。
对于 Spring Boot 2.0,当我们覆盖 WebSecurityConfigurerAdapter
的 configure
方法时,所有现有的安全性都会退缩,我们可以提供自定义安全性。在您的情况下,您只需要验证执行器端点,可以按如下方式完成:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/actuator/**").authenticated();
}
}
application.properties
文件中不需要更改。
我将现有的应用程序从 Spring Boot 1.3 更新到 2.0.1。此应用程序使用 Actuator 并公开 REST 样式 API.
在 Boot 1.3 中,API 无需身份验证即可使用,执行器端点配置为受密码保护:
security.user.name=foo
security.user.password=bar
security-user.role=ADMIN
我按照 configuration changelog 中的记录进行了更新,并将条目从 security.user.name
重命名为 spring.security.user.name
等。
但是当我尝试 curl
我的 API 时,我被拒绝了,因为我没有提供凭据:
在 Spring Blog 中,我找到了如何在详细级别上配置 Spring 安全性的可能解决方案:
http
.authorizeRequests()
// 1
.requestMatchers(EndpointRequest.to("status", "info"))
.permitAll()
// 2
.requestMatchers(EndpointRequest.toAnyEndpoint())
.hasRole("ACTUATOR")
// 3
.requestMatchers(StaticResourceRequest.toCommonLocations())
.permitAll()
// 4
.antMatchers("/**")
.hasRole("USER")
.and()
...
但这比我需要的更细粒度,我正在寻找基于 application.properties
的解决方案。
有没有无需额外代码即可解决此问题的方法?
当您设置 spring.security.user.name
和 spring.security.user.password
时,您正在为整个应用程序(包括 Actuator 端点)配置通过 spring-security
的表单登录。
不幸的是,在 Spring Boot 2.0 中,您无法使用属性设置不同的 username/password 或禁用执行器端点的身份验证。这意味着您必须通过安全配置明确允许执行器端点。
通过 spring-security
,您还可以允许 public 访问您的端点并非常轻松地要求执行器端点的凭据:
@Configuration
public class BasicSecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/**").hasRole("ACTUATOR")
.anyRequest().permitAll();
}
}
(我假设你使用的是 WebMvc,而不是 WebFlux,这有点不同)
确认您在 application.properties
中有以下内容:
spring.security.user.name=user
spring.security.user.password=pass
spring.security.user.roles=ACTUATOR,USER # and others, if you like
management.endpoint.health.roles=ACTUATOR
请参阅 here,了解 Spring 1.x 与 2.0 中 Actuator 差异的快速而准确的解释。
对于 Spring Boot 2.0,当我们覆盖 WebSecurityConfigurerAdapter
的 configure
方法时,所有现有的安全性都会退缩,我们可以提供自定义安全性。在您的情况下,您只需要验证执行器端点,可以按如下方式完成:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/actuator/**").authenticated();
}
}
application.properties
文件中不需要更改。