curl Spring Boot 执行器空响应

0sgqnhkj  于 2022-11-13  发布在  Spring
关注(0)|答案(2)|浏览(249)

Sping Boot 应用程序具有以下Web安全配置:

@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter
{
   @Override
   public void configure(WebSecurity web) throws Exception
   {
      web.ignoring().antMatchers("/actuator/**");
   }

   @Override
   protected void configure(HttpSecurity http) throws Exception
   {
      http.authorizeRequests().antMatchers("/").permitAll();

      http.csrf().disable();
   }
}

application.properites:

management.endpoints.web.exposure.include=health,info,loggers
management.endpoints.web.base-path=/actuator
management.endpoint.health.show-details=always

启动应用程序后,我可以在日志中看到:

[ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 3 endpoint(s) beneath base path '/actuator'

但随后:

bash-5.1$ curl -I --header 'Content-Type: application/json' -X "GET" "http://localhost:8083/actuator/health"
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Thu, 09 Dec 2021 22:26:48 GMT

有没有什么想法为什么它会空出来?

iswrvxsc

iswrvxsc1#

Because:

curl --help all

...says:

...
 -I, --head               Show document info only
...

;)
So with:

curl --header 'Content-Type: application/json' -X "GET" "http://localhost:8083/actuator/health"

(omitting -I ) we get the expected:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   171    0   171    0     0  32540      0 --:--:-- --:--:-- --:--:-- 42750
{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":1991571578880,"free":1684774006784,"threshold":10485760,"exists":true}},"ping":{"status":"UP"}}}
rwqw0loc

rwqw0loc2#

最新的 Spring Boot 需要设置两件事:
1-在application.properties文件中公开端点:

management.endpoints.web.exposure.include=info, health

如果需要,可以使用通配符并公开所有端点

management.endpoints.web.exposure.include=*

2-启用application.properties中的信息环境:

management.info.env.enabled=true

您可以设置自定义信息属性:

info.app.name=Project
info.app.description=A dummy project
info.app.version=1.0.0

相关问题