【问题标题】:Spring Boot 2 Actuator endpoints return 405 on GET requestSpring Boot 2 Actuator 端点在 GET 请求上返回 405
【发布时间】:2020-10-26 15:50:59
【问题描述】:

我正在配置 spring Boot 执行器,但 /actuator 和 /health 端点在每个 GET 请求中返回 405。

我打开了整个安全机制,但它不起作用:

management.endpoints.jmx.exposure.exclude=*
management.endpoints.web.exposure.include=health,info,beans,env
management.endpoint.health.enabled=true
management.security.enabled=false
management.endpoint.beans.cache.time-to-live=10s
management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=GET,POST

我添加了以下配置

@Configuration(proxyBeanMethods = false)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/health", "/actuator").permitAll().and().csrf().disable();
    super.configure(http);
}

}

所以,我按照教程进行操作,看起来很简单,我不明白为什么我会在默认 GET 请求中得到这个 405。当我用 cURL 调用端点时:

 curl http://localhost:8080/sm-integration/health -v --> 405
 curl -X POST http://localhost:8080/sm-integration/health -v --> 400

我在执行 POST 时不理解 400 Bad Request。文档指出 /health 是最基本和开放的端点,应该被称为 GET 调用。那么为什么是 405?

更新 在收到几个答案后,我设置了这样的配置:

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/health", "/actuator/**").permitAll().and().csrf().disable();
}

我将调用更改为 http://localhost:8080/sm-integration/actuator/health

但我仍然得到 405。

在 Tomcat 访问日志中,我确实看到请求已经到达。

10.0.2.2 - - [06/Jul/2020:16:41:06 +0000] "GET /sm-integration/actuator/health HTTP/1.1" 405 5
10.0.2.2 - - [06/Jul/2020:16:41:12 +0000] "GET /sm-integration/actuator/health HTTP/1.1" 405 5
10.0.2.2 - - [06/Jul/2020:16:41:45 +0000] "GET /sm-integration/actuator/health HTTP/1.1" 405 5
10.0.2.2 - - [06/Jul/2020:16:42:18 +0000] "GET /sm-integration/actuator/health HTTP/1.1" 405 5
10.0.2.2 - - [06/Jul/2020:16:43:04 +0000] "GET /sm-integration/actuator HTTP/1.1" 405 5

【问题讨论】:

  • 除非您将执行器端点移动到/,否则/health 是错误的路径。应该是/actuator/health
  • 谢谢,我也尝试使用 /actuator 前缀,得到同样的错误

标签: java spring spring-boot spring-security spring-boot-actuator


【解决方案1】:

好吧,原来我看错了方向。为所有遇到相同问题的人发布此答案。

问题是我介绍Actuator的项目(maven模块)是一个Spring WebService项目。该项目有一个指向“/*”的 DispatcherServlet。在我明确更改它以侦听另一个基本 url 后,Actuator url 可用。

@Bean
public ServletRegistrationBean registerMessageDispatcherServlet(final ApplicationContext applicationContext) {
    final MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/ws/*"); --> was "/*"
}

【讨论】:

    【解决方案2】:

    可能的网址:

    • 应通过 {ip}:{port}/actuator/{endpoint}

      访问 Spring Boot Actuator 端点
    • 在上下文的情况下,路径是为您可以在 {ip}:{port}/{context-path}/actuator/{endpoint}

      访问的应用程序设置的
    • 如果您使用 management.endpoints.web.base-path 自定义了基本路径,您可以通过 {ip}:{port}/{web.base-path}/{endpoint}

      访问它

    删除super.configure(http);,因为它将覆盖您的配置并将"/actuator"更改为"/actuator/**"

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/health", "/actuator/**").permitAll().and().csrf().disable();
    }
    

    【讨论】:

    • 感谢回复,我试过了,还是得到405
    • 你好,玉米,你能用邮递员发出一个GET请求吗?也可以在/actuator 上点击并分享结果
    • 您是否为应用程序中的执行器定义了上下文路径和/或自定义基本路径
    • 我用 Postman 得到了同样的结果。我已经尝试了多个配置,但都没有工作,我开始认为这可能是因为其他一些 spring 配置正在干扰。
    • 我没有在 OP 中配置其他任何东西。
    猜你喜欢
    • 2020-02-18
    • 2020-06-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2019-01-08
    • 2016-06-01
    • 1970-01-01
    相关资源
    最近更新 更多