Spring Boot 2 Actuator 端点在 GET 请求时返回 405

Coe*_*men 1 java spring spring-security spring-boot spring-boot-actuator

我正在配置 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
Run Code Online (Sandbox Code Playgroud)

我添加了以下配置

@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);
}
Run Code Online (Sandbox Code Playgroud)

}

因此,我按照教程进行操作,看起来非常简单,我不明白为什么在默认 GET 请求上收到此 405。当我使用 cURL 调用端点时:

 curl http://localhost:8080/sm-integration/health -v --> 405
 curl -X POST http://localhost:8080/sm-integration/health -v --> 400
Run Code Online (Sandbox Code Playgroud)

我不明白在执行 POST 时出现 400 Bad Request。文档指出 /health 是最基本和开放的端点,应该作为 GET 调用来调用。那么为什么是405呢?

更新 收到几个答案后,我设置了如下配置:

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

我将调用更改为 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
Run Code Online (Sandbox Code Playgroud)

Coe*_*men 6

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

问题是我引入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 "/*"
}
Run Code Online (Sandbox Code Playgroud)