【问题标题】:Run unit tests on controllers that require authentication在需要身份验证的控制器上运行单元测试
【发布时间】:2015-06-24 14:43:11
【问题描述】:

我有一个 Spring Boot 应用程序,它需要登录才能执行某些操作。我正在尝试使用MockMvc 测试它们,但它似乎不起作用。我不断收到状态为 403(禁止)的 HTTP 响应。可能是认证部分有问题。

我已尝试关注documentation,但无法正常工作。

这是我当前的测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@WebIntegrationTest("server.port = 8093")
public class PasswordChangeTests {
    @Autowired
    private EmbeddedWebApplicationContext webApplicationContext;

    @Autowired
    private UserRepository userRepository;

    private MockMvc mockMvc;

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

     @Test
     public void changePasswordWorks() throws Exception {
         // Send password change request
         PasswordChangeRepresentation passwordChange = new PasswordChangeRepresentation(DefaultUsers.Admin.getPassword(), "12345678");
         mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/password/change")
                 .content(new ObjectMapper().writeValueAsString(passwordChange))
                 .contentType(MediaType.APPLICATION_JSON)
                 .accept(MediaType.APPLICATION_JSON))
                 .andExpect(status().isOk());

         // Check that the password has been changed
         User user = this.userRepository.findByUsername(DefaultUsers.Admin.getEmail());
         Assert.assertEquals(user.getPassword(), "12345678");
    }
}

对不起,如果我遗漏了一些明显的东西。这是我第一次使用 Spring Boot。

【问题讨论】:

  • 我很好奇你的springSecurity() 方法。能否请您显示该方法的方法体?
  • springSecurity() 方法由 Spring Security 提供。它添加了 springSecurityFilterChain 过滤器,并确保将测试支持集成到过滤器中。

标签: java spring-mvc spring-security spring-boot mockmvc


【解决方案1】:

您需要指定要以哪个用户身份运行测试。您有几个选项(每个选项都是详细文档的链接):

@WithMockUser

此选项将创建一个假用户(即该用户不需要存在于数据存储中)。这种方法的问题是,如果您的应用程序依赖于自定义用户实现,您可能会遇到类转换异常。如果您没有从自定义 UserDetailsS​​ervice 返回自定义类型,那么此解决方案应该可以正常工作。

 @Test
 @WithMockUser(username="admin",roles={"USER","ADMIN"})
 public void changePasswordWorks() throws Exception {

@WithUserDetails

如果您实现了返回自定义 UserDetails 实现的自定义 UserDetailsS​​ervice,则此解决方案可能适合您。

要使其工作,您需要将 UserDetailsS​​ervice 公开为 Bean,并且用户必须存在。例如:

 @Test
 @WithUserDetails("admin")
 public void changePasswordWorks() throws Exception {

@WithSecurityContext

这是两全其美的,但需要一些额外的设置。如果您有自定义 UserDetailsS​​ervice 返回 UserDetails 的自定义实现并且不希望用户必须存在,您可以使用此方法。我会让您阅读有关此设置的文档,因为它有点冗长且有据可查。

Using a RequestPostProcessor

如果您不喜欢注释,您可以使用 RequestPostProcessor。例如:

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;

 ...

 @Test
 public void changePasswordWorks() throws Exception {
     // Send password change request
     PasswordChangeRepresentation passwordChange = new PasswordChangeRepresentation(DefaultUsers.Admin.getPassword(), "12345678");
     mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/password/change")

             // ADD this line
             .with(user("admin").roles("USER","ADMIN"))

             .content(new ObjectMapper().writeValueAsString(passwordChange))
             .contentType(MediaType.APPLICATION_JSON)
             .accept(MediaType.APPLICATION_JSON))
             .andExpect(status().isOk());

     // Check that the password has been changed
     User user = this.userRepository.findByUsername(DefaultUsers.Admin.getEmail());
     Assert.assertEquals(user.getPassword(), "12345678");
}

【讨论】:

  • 我已经尝试了所有这些但它不起作用。我在控制器中有一个 @Secured("ROLE_USER") 注释,我无法编写一个通过不同角色的测试来失败。有什么建议吗?
  • 从 Spring-Security ~5.4.x 版本开始,您可以通过设置 setupBefore = TestExecutionEvent.TEST_EXECUTION 来强制 @WithUserDetails@BeforeEach Junit5 注释之后启动。完整示例:@WithUserDetails(value ="LOGIN", setupBefore = TestExecutionEvent.TEST_EXECUTION)。您还可以创建带有 @PostConstruct 注释的方法,这将节省您的用户。在这些情况下,您可以直接在测试类中提供用户。 link Spring-Security-Documentation
猜你喜欢
  • 1970-01-01
  • 2016-04-03
  • 2022-01-24
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多