java - 如何测试受保护的 Spring Controller
问题描述
我目前有一个使用 Spring Boot 2、Spring MVC、Spring Data/JPA 和 Thymeleaf 构建的应用程序。
我正在编写一些单元/集成测试,并且我想测试控制器,该控制器由 SpringSecurity 保护,该数据库由注册用户提供支持。
在这里测试它的最佳方法是什么?我没有成功地尝试过其中一些,比如使用@WithMockUser
.
编辑:只是提醒我没有测试@RestController
s。我直接@Controller
在我的测试类上注入 a 并调用它的方法。如果没有 Spring Security,它就可以正常工作。
一个例子:
@Controller
public class SecuredController {
@GetMapping("/")
public String index() {
return "index";
}
}
该/
路径由 Spring Security 保护,通常会重定向到/login
以对用户进行身份验证。
我的单元测试看起来像这样:
@WebMvcTest(controllers = SecuredController.class)
class SecuredControllerTest {
@Autowired
private SecuredController controller;
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser(username = "user", password = "pass", roles = {"USER"})
public void testAuthenticatedIndex() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(print());
}
}
我得到的第一个错误是要求我注入我的 UserDetailsService 实现,这是我想避免的。但是,如果我确实注入了服务,则测试可以工作,但返回 404 而不是 200。
有任何想法吗?
解决方案
您将需要通过导入您的WebSecurityConfigurerAdapter
类将您的安全配置添加到 Spring 上下文中。
@WebMvcTest(controllers = SecuredController.class)
@Import(SecuredControllerTest.Config.class)
class SecuredControllerTest {
@Configuration
@EnableWebSecurity
static class Config extends MyWebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("pa$$").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("pa$$").roles("ADMIN");
}
}
...
}
嵌入式static class Config
只是为了改变我们从哪里获取用户,在这种情况下一个inMemoryAuthentication
就足够了。
推荐阅读
- swift - 滚动时如何一次旋转表格视图单元格中的所有视图?
- python - 如何停止在 django_restframework 中获取重复数据?
- sql-server - 如何删除临时表
- python-3.x - pyinstaller v3.5 执行失败
- char - 字符串索引超出范围,是否有比较两个字符串的正确方法?
- sql - 从主表中获取所有信息,其中子表中的日期字段超过 365 天 - 只是稍微复杂一点
- javascript - 如何比较当前活动标签的 URL
- c# - AS400 锁定未使用的文件
- r - Markdown > html : 将表格标题居中
- css - Google amp-lightbox:内容高于屏幕,无法滚动到顶部