打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring Boot Actuator:介绍和使用
IDDescriptionEnabled by default
heapdump返回一个GZip压缩的hprof堆dump文件Yes
jolokia通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用)Yes
logfile返回日志文件内容(如果设置了logging.file或logging.path属性的话), 支持使用HTTP Range头接收日志文件内容的部分信息                         Yes
prometheus      以可以被Prometheus服务器抓取的格式显示metrics信息Yes

 

如果要启用/禁用某个端点,可以使用management.endpoint.<id>.enabled属性:

management:  endpoint:    shutdown:      enabled: true

 

另外可以通过management.endpoints.enabled-by-default来修改全局端口默认配置,比如下面禁用所有端点只启用info端点:

management:  endpoints:    enabled-by-default: false  endpoint:    info:      enabled: true

 

上面是启用/禁用(enable)某个端点,如果使某个端点暴露(exposure)出来,还需要再配置,默认情况下所有端点在JMX下是全部公开的,在Web下只公开/health和/info两个端点。下面是默认配置:

PropertyDefault
management.endpoints.jmx.exposure.exclude         - 
management.endpoints.jmx.exposure.include '*'
management.endpoints.web.exposure.exclude -                  
management.endpoints.web.exposure.include   info, health                                                             

 

下面的例子是Web下公开所有端点:

management:  endpoints:    web:      exposure:        include: '*'

 

保护Actuator HTTP端点:

最简单的方式,就是在pom.xml中添加spring-boot-starter-security。由SpringBoot Security的特性可知,系统会自动给我们创建login/logout page,还有一个user和password,此外系统还会自动给我配置一个ManagementWebSecurityConfigurerAdapter(extends WebSecurityConfigurerAdapter),配置Actuator各个Endpoint的权限。

当然我们也可以自定义一个WebSecurityConfigurerAdapter配置自己的user和authority。

package com.mytools;import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;import org.springframework.boot.actuate.health.HealthEndpoint;import org.springframework.boot.actuate.info.InfoEndpoint;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.factory.PasswordEncoderFactories;import org.springframework.security.crypto.password.PasswordEncoder;@Configurationpublic class MyWebSecurityConfigurer extends WebSecurityConfigurerAdapter {    @Bean    public PasswordEncoder passwordEncoder() {        return PasswordEncoderFactories.createDelegatingPasswordEncoder();    }    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        //@formatter:off        PasswordEncoder encoder = new BCryptPasswordEncoder();        auth.inMemoryAuthentication()            .withUser("user1").password("{bcrypt}" + encoder.encode("password1")).roles("ADMIN","EUREKA")            .and()            .withUser("user2").password("{bcrypt}" + encoder.encode("password2")).roles("EUREKA");        //@formatter:on    }    @Override    protected void configure(HttpSecurity http) throws Exception {        // comes from ManagementWebSecurityAutoConfiguration and ManagementWebSecurityConfigurerAdapter        //@formatter:off        http.authorizeRequests()                .requestMatchers(EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class)).permitAll()                .anyRequest().authenticated()                .and()            .formLogin().and()            .httpBasic();        //@formatter:on    }}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
springcloud Alibaba 2021.0.1.0 springboot 2.7.0 整合 Swagger3.0 报错解决方案
【安全鉴权】Spring Boot Actuator如何进行安全鉴权?还得是这样。
应用监控
原始性能表格 - Spring Boot 2 Webflux vs. Spring Boot 1
Spring Boot使用Spring Security实现权限控制
SpringBoot自动配置原理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服