[spring-projects/spring-boot]Actuator的EndpointRequest不考虑server.servlet.path

2024-04-23 75 views
3

在application.properties中设置server.servlet.path=/spring

现在请注意 EndpointRequest.* 不匹配任何内容。例如,对/spring/actuator/health 的请求不匹配(/actuator/health但对 /health 的请求确实匹配 - 即使它会返回 404)。

回答

4
4

@candrews 我不认为是这样。RequestMatcher上的匹配,request.getServletPath在本例中(即使请求是/spring/actuator/health)是/actuator/health。如果您希望我们进一步调查此问题,请提供一个简单的示例,我们可以用它来重现该问题。

1

使用https://start.spring.io/创建一个新项目,包括 Web、Security 和 Actuator 在 application.properties 中:server.servlet.path=/spring 创建此类:

package com.example.demo;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                // this line works
//              .requestMatchers(new AntPathRequestMatcher("/spring/actuator/health")).permitAll()

//               this line does not work, but should: https://github.com/spring-projects/spring-boot/issues/12934
                .requestMatchers(EndpointRequest.to("health")).permitAll()
                .anyRequest().denyAll()

                .and()
            .formLogin()
                .loginPage("/spring/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

}

运行它,现在转到http://localhost:8080/spring/actuator/health

我希望得到一个 JSON 结果,健康检查状态为“UP”,实际上我得到了 302 重定向。在 WebSecurityConfig 中,如果您注释掉第一个注释行,并取消注释第二个注释行,然后再次运行此测试,您将获得预期结果。

5

啊,我没有意识到您正在配置的属性是server.servlet.path而不是server.servlet.context-path。问题标题说 context-path ,如果这就是你想要做的,你需要设置server.servlet.context-path.EndpointRequest在这种情况下会起作用。但我们需要修复 if设置EndpointRequest不匹配的情况。server.servlet.path

8

我对标题中的错误感到抱歉:( 不过,我很高兴我们现在在同一页面上!谢谢。

1

@mbhave在webfluxserver.servlet.context-path的 2.0.1.RELEASE 中也被忽略

我有以下内容application.yml

management:
  server:
    port: ${MANAGEMENT_PORT:8889}
    servlet:
      context-path: "/management"

info端点映射到/actuator/info,而不是/management/actuator/info

2018-05-03 12:38:40.235  INFO 1 --- [           main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
8

@n0mer,它的名称中包含的事实servlet应该强烈表明它无法在不需要 servlet API 的环境中工作。如果您有更多问题,请在 StackOverflow 上提问或在 Gitter 上与我们聊天。

3

@n0mer 你所说的不是一个错误。根据设计,特定于 servlet 的属性对 WebFlux 应用程序没有影响。它也与此问题无关,该问题专门与EndpointRequest用于配置 Spring Security 的类有关。

5

我在 2.0.1-RELEASE 中遇到这个问题,但仅限于 jolokia 端点。所有其他端点均按预期工作。

6

我仍然面临这个 jolokia 端点问题...使用当前的 2.0.2.BUILD-SNAPSHOT 版本。

//actuator specific version
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version:'2.0.2.BUILD-SNAPSHOT'
compile group: 'org.springframework.boot', name: 'spring-boot-actuator', version:'2.0.2.BUILD-SNAPSHOT'

不是应该修一下吗?

8

@gmcouto 的问题JolokiaEndpoint是不同的。它似乎没有包含在RequestMatcher创建者中EndpointRequest#including(或从RequestMatcherif ( EndpointRequest#excluded) 中排除)。我在这里为此创建了一个单独的问题。

如果您发现 Jolokia 端点的错误与正在设置的 servlet-path 有关(这是此处的原始问题),请提供重现该问题的示例,我们可以重新讨论此问题。

6

如果您在应用程序上设置了 server.servlet.path 属性,则所有执行器端点都将相应地移动到该属性,除了 jolokia 端点。

jolokia 端点如何: http://host:port/context-path/actuator/jolokia

jolokia 端点应该是: http://host:port/context-path/servlet-path/actuator/jolokia

其他端点的示例: http://host:port/context-path/servlet-path/actuator/health http://host:port/context-path/servlet-path/actuator/auditevents http://host:port /上下文路径/servlet-path/actuator/beans

样本 缺陷执行器-jolokia.zip

我已将相同的信息添加到另一个缺陷中。不知道你喜欢如何跟踪这个。