Spring 引导 2 执行器端点 return GET 请求时为 405

Spring Boot 2 Actuator endpoints return 405 on GET request

我正在配置 spring 引导执行器,但 /actuator 和 /health 端点 return 在每个 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

可能的网址:

  • Spring Boot Actuator 端点应在 {ip}:{port}/actuator/{endpoint}

    处访问
  • 在上下文的情况下,路径是为您可以在 {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();
}

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

问题是我引入Actuator的项目(maven模块)是一个SpringWebService项目。该项目有一个指向“/*”的 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 "/*"
}