gpt4 book ai didi

java - Spring Boot 的执行器端点在测试期间可用

转载 作者:行者123 更新时间:2023-11-30 06:39:32 24 4
gpt4 key购买 nike

我有一个包含执行器的 Web 应用程序,该应用程序在上下文路径 /manage 下公开,并且仅通过 Spring Security 集成提供给具有“管理员”角色的用户使用。事实上,该应用程序确实按预期工作。但是,我正在努力测试 Web 应用程序,即使用 Spring Boot 的工具编写集成测试。

运行如下测试,加载 spring 上下文,我注意到执行器端点未注册。

@RunWith(SpringRunner.class)
@DirtiesContext
@SpringBootTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, WithSecurityContextTestExecutionListener.class})
@WebAppConfiguration
@Slf4j
public class ActuatorControllerTest {

private MediaType contentTypeJSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), StandardCharsets.UTF_8);

protected MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() throws Exception {
this.webApplicationContext.getBean(HealthEndpoint.class).setEnabled(true);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
}

@Test
@WithMockUser(username="test",roles={"developers"})
public void user_without_role_administrator_should_not_be_able_to_access_actuator() throws Exception {
mockMvc.perform(get("/manage/health").with(csrf()))
.andExpect(status().isForbidden());
}

@Test
@WithMockUser(username="admin",roles={"administrator"})
public void user_with_role_administrator_should_be_able_to_access_actuator() throws Exception {
mockMvc.perform(get("/manage/health")
.with(csrf())
// I also tried the following approach which miserably failed
// .with(user(buildUserDetails()))
).andExpect(status().isOk());
}


private UserDetails buildUserDetails() {
return User.withUsername("admin").password("admin").roles("administrators").build();
}
}

日志文件片段

2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'environmentEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'healthEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'beansEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'infoEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'loggersEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'metricsEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'traceEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'dumpEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'autoConfigurationReportEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'shutdownEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'configurationPropertiesReportEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'endpoints-org.springframework.boot.actuate.endpoint.EndpointProperties': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Rejected bean name 'org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration': no URL paths identified

application.properties 中的相关片段如下所示。

management.port=8080
management.context-path=/manage
management.security.enabled=false
endpoints.health.sensitive=false
management.health.db.enabled=false
management.health.defaults.enabled=true
management.health.diskspace.enabled=true
management.security.roles=administrators

有人可以帮助我,让我知道我在这里做错了什么以及如何解决被拒绝的 bean-name 问题以继续进行吗?

就我而言,Spring Boot 和 Actuator 的版本是 1.5.2.RELEASE

最佳答案

如何在 pom 中添加执行器依赖项?您是否添加了任何依赖范围? 如果您已添加如下内容,请删除编译或将其更改为测试

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>compile</scope>
</dependency>

关于java - Spring Boot 的执行器端点在测试期间可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44625330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com