为啥 Spring Boot Actuator 返回 404 而不是 401?

Posted

技术标签:

【中文标题】为啥 Spring Boot Actuator 返回 404 而不是 401?【英文标题】:Why does Spring Boot Actuator return a 404 instead of 401?为什么 Spring Boot Actuator 返回 404 而不是 401? 【发布时间】:2016-04-27 23:50:28 【问题描述】:

这是一个非常愚蠢的问题,但我不明白为什么当我尝试访问安全端点时执行器给我一个 404 而不是 401。

我添加了依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

并禁用非敏感端点的安全性

security.basic.enabled=false

现在,我可以访问应用程序和/health(的残缺版本),但是当我转到/metrics 时,它会给我一个WWW-Authenticate 标头,很好,但是带有404,所以我的浏览器没有'不提示我输入用户名和密码。

$ curl -v http://localhost:8081/metrics
* STATE: INIT => CONNECT handle 0x6000572d0; line 1090 (connection #-5000)
* Added connection 0. The cache now contains 1 members
*   Trying ::1...
* STATE: CONNECT => WAITCONNECT handle 0x6000572d0; line 1143 (connection #0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000572d0; line 1240 (connection #0)
* STATE: SENDPROTOCONNECT => DO handle 0x6000572d0; line 1258 (connection #0)
> GET /metrics HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.45.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x6000572d0; line 1337 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x6000572d0; line 1464 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x6000572d0; line 1474 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Strict-Transport-Security: max-age=31536000 ; includeSubDomains
< WWW-Authenticate: Basic realm="Spring"
< Content-Length: 0
< Date: Thu, 21 Jan 2016 15:55:12 GMT
<
* STATE: PERFORM => DONE handle 0x6000572d0; line 1632 (connection #0)
* Curl_done
* Connection #0 to host localhost left intact

以及application.properties中的相关属性

# Spring Boot Actuator
management.address=127.0.0.1
management.port=8081
security.basic.enabled=false
security.user.name=admin
security.user.password=a1b2c3

为什么会这样?可以改吗?

【问题讨论】:

你能发布你的整个配置文件吗?您应该为所有端点(甚至是组成的端点)获得 401,除了那些明确不敏感的端点,例如 /health @adamr 我添加了我拥有的属性,如果你是这个意思的话。 假设您的应用程序在不同的端口上运行,您是否可以尝试在与您的应用程序相同的端口上运行执行器并查看指标是否有效? 【参考方案1】:

你已经设置了management.context-path=/actuator,所以所有的Actuator端点都会以这个为前缀。你的网址应该是localhost:8080/actuator/metrics

【讨论】:

抱歉,在第一次编辑中,我使用management.context-path=/actuator 发布了更新版本的属性。那不应该在那里,我删除了它。【参考方案2】:

确保您已添加 spring-boot-starter-web:

  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

【讨论】:

【参考方案3】:
1. endpoints.health.enabled=false , in application.properties
This was causing issue at my end. I removed this property from application.properties

2.In pom.xml it should be compile in <scope> tag.
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <scope>compile</scope>
</dependency>

【讨论】:

编译是默认范围。所以如果没有范围标签,它就会编译。

以上是关于为啥 Spring Boot Actuator 返回 404 而不是 401?的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置

无法访问 Spring Boot Actuator“/actuator”端点

Spring Boot -- actuator

spring-boot-starter-actuator监控接口详解

spring-boot 监控 Actuator

Spring boot Actuator