【发布时间】:2020-06-01 23:36:47
【问题描述】:
我有一个带有 Spring Security 微服务的 Spring Boot 2,我已经配置了 Micometer/Spring Actuator。当我在 antMatcher("/actuator/**") 端点上 permitAll() 时,一切都很好。我可以通过正确配置的 Prometheus yaml 文件检索 Prometheus 指标。
但是,我的微服务不在防火墙后面,因此向世界开放。我只希望 Prometheus 能够访问我的微服务“/actuator/prometheus”端点。
我有以下配置:
在我的 Spring Boot 2 微服务ResourceServerConfig 类中:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private JdbcTokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("springsecurity").tokenStore(tokenStore);
}
public void configure(HttpSecurity http) throws Exception {
http.anonymous().and().authorizeRequests()
.antMatchers("/actuator/**").hasRole("ENDPOINT_ADMIN")
.antMatchers("/**").authenticated();
}
对于我拥有的application.properties 文件:
spring.security.user.name=user
spring.security.user.password=password
spring.security.user.roles=ENDPOINT_ADMIN
# For Prometheus (and other data loggers)
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
然后对于我的 Prometheus YAML 文件,我有这个:
global:
scrape_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: 'myservice'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080']
basic_auth:
username: 'user'
password: 'password'
当我在 Prometheus 中转到 /targets 时,我得到“服务器返回 HTTP 状态 401”。
我完全认为我对某些事情的理解并不完全正确。有没有办法让我正确地做到这一点?非常感谢。
【问题讨论】:
-
我相信您希望您的端点受到基本身份验证的保护?如果这是真的,您可以尝试编辑以更好地解释它。
标签: java spring-security oauth prometheus spring-boot-actuator