在需要身份验证的控制器上运行单元测试

aoc*_*via 9 java spring-mvc spring-security spring-boot mockmvc

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

我试过按照文档,但我无法使它工作.

这是我目前的测试代码:

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

对不起,如果我错过了一些明显的事 这是我第一次使用弹簧靴.

Rob*_*nch 24

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

@WithMockUser

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

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

@WithUserDetails

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

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

 @Test
 @WithUserDetails("admin")
 public void changePasswordWorks() throws Exception {
Run Code Online (Sandbox Code Playgroud)

@WithSecurityContext

这是两个世界中最好的,但需要一些额外的设置.如果您有一个自定义UserDetailsS​​ervice返回UserDetails的自定义实现,并且不希望用户必须存在,则可以使用此方法.我会让你阅读有关此设置的文档,因为它有点冗长且有详细记录.

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